使嵌套for循环的数量为变量 - VBA

时间:2016-10-09 02:15:43

标签: vba excel-vba excel

我开发了像这样的代码

For i = 1 To 50

    For j = i + 1 To 50

        For k = j + 1 To 50

            ..........................
            'Here I have someother code
            ..........................

        Next k

    Next j

Next i

所以这是一个嵌套的for循环。此代码中有3个for循环。 现在我想要的是,我想让for循环的数量成为一个变量。举个例子,我有变量调用NumberOfForLoops。因此,如果NumberOfForLoops = 3那么应该有3个for循环。如果NumberOfForLoops = 8那么应该有8个for循环。我无法找到如何做到这一点。

我认为最好解释一下我想要实现的目标。

我试图找到可能的组合

对于3种组合我写了这段代码

    Dim WS_Data As Worksheet
    Dim WS_Result As Worksheet

    Set WS_Data = Worksheets("Data")
    Set WS_Result = Worksheets("Result")

    Dim i As Long
    Dim j As Long
    Dim k As Long
    Dim EnteringRow As Long

    EnteringRow = 1

    For i = 1 To 5
        For j = i + 1 To 5
            For k = j + 1 To 5
                WS_Result.Range("A" & EnteringRow).Value = WS_Data.Range("A" & i).Value
                WS_Result.Range("B" & EnteringRow).Value = WS_Data.Range("A" & j).Value
                WS_Result.Range("C" & EnteringRow).Value = WS_Data.Range("A" & k).Value
                EnteringRow = EnteringRow + 1
            Next k
        Next j
    Next i

它给了我以下结果

Input and Outcome

但现在我需要的是我想让我从列表中选择的项目数量动态。据我所知,我需要使用递归概念。 所以我根据h2so4的答案修改了代码。

这是修改后的代码

    Sub test()
        Dim WS_Data As Worksheet
        Dim WS_Result As Worksheet
        Dim WS_Temp As Worksheet

        Set WS_Data = Worksheets("Data")
        Set WS_Result = Worksheets("Result")
        Set WS_Temp = Worksheets("Temp")

        ResultRow = 1
        NofL = 3
        Nestedloop WS_Data, WS_Result, WS_Temp, ResultRow, NofL, 1, 5, 1

    End Sub
    Sub Nestedloop(WS_Data, WS_Result, WS_Temp, ResultRow, NofL, jmin, jmax, level)

        For j = jmin To jmax
            WS_Temp.Cells(1, level) = j
            'your code when a value of j is set
            If level < NofL Then
                Nestedloop WS_Data, WS_Result, WS_Temp, ResultRow, NofL, jmin + 1, jmax, level + 1
            Else
                'your code when the number of loops is reached

                For i = 1 To NofL
                    WS_Result.Cells(ResultRow, 0 + i).Value = WS_Data.Range("A" & WS_Temp.Cells(1, i).Value).Value
                Next i
                ResultRow = ResultRow + 1
            End If
        Next j
    End Sub

我用了一张临时表。这是我得到的结果。

Result got

很难理解错误。

4 个答案:

答案 0 :(得分:2)

另一个带有递归子的提议,模拟嵌套循环

Sub test()
    NofL = 4
    Nestedloop NofL, 1, 50, 1
End Sub
Sub Nestedloop(NofL, jmin, jmax, level)
    For j = jmin To jmax
        Cells(1, level) = j
        'your code when a value of j is set
        If level < NofL Then
            Nestedloop NofL, jmin + 1, jmax, level + 1
        Else
        'your code when the number of loops is reached
        End If
    Next j
End Sub

答案 1 :(得分:0)

您的问题可以通过多种方式阅读。如果您正在寻找有助于生成样板代码的工具,可能是这样的:

'In the following code, if vars is missing, successive loop indices are "i", "j", "k", etc
'otherwise, vars is treated as the loop vars and should be passed as a 0-based array with
'depth strings, where depth is how deeply nested the loops are

Function NestedFors(lim As Long, depth As Long, Optional vars As Variant) As String
    Dim i As Long, n As Long
    Dim codeShell As String

    If IsMissing(vars) Then
        vars = Split("i j k l m n o p q r s t u v w x y z") 'should be overkill -- if not, you deserve a runtime error!
    End If

    codeShell = "For " & vars(0) & " = 1 To " & lim & vbCrLf

    For i = 1 To depth - 1
        codeShell = codeShell & String(i, vbTab)
        codeShell = codeShell & "For " & vars(i) & " = " & vars(i - 1) & " + 1 To " & lim & vbCrLf
    Next i

    codeShell = codeShell & String(depth, vbTab) & "'----- Insert code here ------" & vbCrLf

    For i = depth - 1 To 1 Step -1
        codeShell = codeShell & String(i, vbTab) & "Next " & vars(i) & vbCrLf
    Next i

    codeShell = codeShell & "Next " & vars(0) & vbCrLf

    NestedFors = codeShell
End Function

然后,例如,如果您键入

?nestedfors(50,4)
<立即窗口中的

可以获得以下内容(可以将其复制粘贴到上面的代码窗口中:

For i = 1 To 50
    For j = i + 1 To 50
        For k = j + 1 To 50
            For l = k + 1 To 50
                '----- Insert code here ------
            Next l
        Next k
    Next j
Next i

答案 2 :(得分:-1)

编辑以生成嵌套循环

Option Explicit

Sub main()
    Dim NumberOfForLoops As Long

    NumberOfForLoops = 3
    ForLoops NumberOfForLoops, 1, 50
End Sub

Sub ForLoops(nLoops As Long, jMin As Long, jMax As Long, Optional level As Long)
    Dim j As Long
    If level = 0 Then level = 1
    For j = jMin To jMax
        If level < nLoops Then
            ForLoops nLoops, jMin + 1, jMax, level + 1
        Else
            'your "someother" code
        End If
    Next j
End Sub

答案 3 :(得分:-2)

如果要运行第一个嵌套循环“NumberOfForLoops”次,只需将“To”部分更改为NumberOfForLoops。

For i = 1 To 50

    For j = 1 To NumberOfForLoops


        For k = j + 1 To 50

            ..........................
            'Here I have someother code
            ..........................

        Next k

    Next j

Next i