对于每个嵌套在另一个中对于每个不能完全正常工作

我写了一些VBA代码来做几件事: 1)。检索3个范围:字符串范围,输出范围,子字符串(查找)范围 2)。遍历“字符串”范围以找到子字符串列表之一 3)。将发现的子字符串添加到输出范围

我的代码将仅对子字符串范围内的第一个单元格执行这些操作。我是否不允许嵌套每个循环?

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:",Type:=8)
    If TypeName(sRRg) = "Nothing" Then Exit Sub

    Set sFRg = Application.InputBox("Please select substring cell:",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,cFindLength))
                    End If
                Next cNumber

            Next ssF1Rg

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

    Next sRg

End Sub
lvjiesjtu 回答:对于每个嵌套在另一个中对于每个不能完全正常工作

您问题的技术答案:是的,您可以在循环中运行循环,但是我意识到您的最终问题是:“为什么不按照我的意愿去做?!?”。由于我不理解您的意图,因此不确定答案。我在即时窗口中添加了一些断点和一些打印内容,以证明它正在运行循环。我还清理了一些零件。如果您在进行任何操作时都运行此命令,则可能会发现问题。

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:",Type:=8)
        If sRRg Is Nothing Then Exit Sub

    Set sFRg = Application.InputBox("Please select substring cell:",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,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
本文链接:https://www.f2er.com/3053825.html

大家都在问