Home » excel » excel – For Each nested in another For Each not fully working

excel – For Each nested in another For Each not fully working

Posted by: admin April 23, 2020 Leave a comment

Questions:

I wrote some VBA code to do a couple things:
1). Retrieve 3 ranges: Strings Range, Output Range, Substring (Find) Range
2). Loop through the Strings range to find one of a list of Substrings
3). Add the discovered substring to the output range

My code will only perform these actions for the first cell in the substring range. Am I not allowed to nest For Each loops?

Sub ExtrSubString()

    Dim sRg As Range
    Dim sDRg As Range
    Dim sRRg As Range
    Dim sFRg As Range
    Dim ssF1Rg As Range

    Dim cCellLength As Integer
    Dim cFindLength As Integer
    Dim cNumber As Integer

    Dim strList As String
    Dim sTitleId As String

    Dim nI As Integer

    sTitleId = "Substring Extraction"
    Set sDRg = Application.InputBox("Please select text strings:", xTitleId, "", Type:=8)
    If TypeName(sDRg) = "Nothing" Then Exit Sub

    Set sRRg = Application.InputBox("Please select output cell:", xTitleId, "", Type:=8)
    If TypeName(sRRg) = "Nothing" Then Exit Sub

    Set sFRg = Application.InputBox("Please select substring cell:", xTitleId, "", Type:=8)
    If TypeName(sFRg) = "Nothing" Then Exit Sub

    sI = 0
    strNumber = ""


    For Each sRg In sDRg
        nI = nI + 1

            For Each ssF1Rg In sFRg

                cCellLength = Len(sRg)
                cFindLength = Len(ssF1Rg)

                For cNumber = 1 To cCellLength
                    If ssF1Rg = (Mid(sRg, cNumber, cFindLength)) Then
                        strList = (Mid(sRg, cNumber, cFindLength))
                    End If
                Next cNumber

            Next ssF1Rg

        sRRg.Item(nI) = strList
        strList = ""

    Next sRg

End Sub
How to&Answers:

The technical answer to your question: Yes, you can run loops within loops, however I realize your ultimate question is “why isn’t it doing what I want?!?”. I’m not sure to the answer to that as I don’t understand your intent. I’ve included some break points and some prints to the immediate window to prove it is running the loops. I also cleaned up a couple parts. If you run this on whatever you’re working on, you might figure out your issue.

Sub ExtrSubString()
Const turnOnBreakpoints As Boolean = True 'set this to false to run fully through code

    Dim sRg As Range, sDRg As Range, sRRg As Range, sFRg As Range, ssF1Rg As Range

    Dim cCellLength As Long, cFindLength As Long, cNumber As Long, ni As Long
    Dim strList As String

    Const stitleid As String = "Substring Extraction" 'not doing anything

    Set sDRg = Application.InputBox("Please select text strings:", xTitleId, "", Type:=8)
        If sDRg Is Nothing Then Exit Sub 'cleaner test for nothing

    Set sRRg = Application.InputBox("Please select output cell:", xTitleId, "", Type:=8)
        If sRRg Is Nothing Then Exit Sub

    Set sFRg = Application.InputBox("Please select substring cell:", xTitleId, "", Type:=8)
        If sFRg Is Nothing Then Exit Sub

    'these aren't doing anything
    'sI = 0
    'strNumber = ""

    For Each sRg In sDRg.Cells
        ni = ni + 1

            For Each ssF1Rg In sFRg.Cells
                Dim loopssF1rg As Long 'used to count the loops for illustration

                cCellLength = Len(sRg)
                cFindLength = Len(ssF1Rg)

                For cNumber = 1 To cCellLength

                    'you're trying to capture part of string here and do what?
                    If ssF1Rg = (Mid(sRg.Value, cNumber, cFindLength)) Then
                        strList = (Mid(sRg.Value, cNumber, cFindLength))
                    End If
                    Debug.Print "loop Cnumber run " & cNumber
                    If turnOnBreakpoints Then Stop
                Next cNumber

                loopssF1rg = loopssF1rg + 1
                If turnOnBreakpoints Then Stop
                Debug.Print "loopssF1rg run " & loopssF1rg
            Next ssF1Rg

        'what is the intent here?
        sRRg.Item(ni) = strList
        strList = ""
    Debug.Print "srg loop run " & ni
    Next sRg

End Sub