嵌套循环无法在VBA中正确循环-一种有效或另一种有效

时间:2018-07-16 15:16:53

标签: excel vba excel-vba excel-2010 nested-loops

我有一个项目正在逐步进行。对于此步骤,我试图使程序在一定数量的行(我正在使用For循环)中重复一定次数(我尝试使用“直到循环”)。 。行数比我希望程序循环的次数大(大得多)。我尝试使用两个循环的原因是因为我想在i范围的每一行中搜索各种条件,但我只想更改前n个匹配项。因为我不知道比赛在哪里或相距多远,所以我需要搜索9000+的整个范围。

到目前为止,仅for循环有效,但是我不知道如何成功添加Do Before循环。我在内部和外部都尝试过For循环,但没有一个起作用。

在此示例中,for循环位于内部。本来应该做9,却做了9,057(E的值):

    Sub Hilight()

Dim usch As Worksheet
Set usch = Worksheets("USCH attributes")
Dim m As Worksheet
Set m = Worksheets("Maps")

Dim i As Integer
Dim E As Integer
Dim n As Integer

Dim s1 As String
Dim s2 As String
Dim s3 As String
s1 = Range("U1").Value
s2 = Range("V1").Value
s3 = Range("W1").Value

Dim rU As String
Dim rE As String
Dim rA As String
rU = m.Range("D4").Value
rE = m.Range("D3").Value
rA = m.Range("D2").Value

E = Range("S1").Value
n = 0
        Do Until n >= 9  
        For i = 1 To E
            n = n + 1
            Debug.Print n
            If Cells(i, 15).Value = s3 And Cells(i, 13).Value = rA And Cells(i, 11) = "" Then   
                Cells(i, 15).Interior.ColorIndex = 0
            End If     
        Next i
        Loop
End Sub

下一个外部带有for循环的示例看起来更好一些,我猜呢?调试最多只能打印9个,所以很好。不幸的是,For循环似乎根本没有运行,因为没有改变颜色。 (仅粘贴内部代码,因为所有暗调都相同)

 For i = 1 To E
    Do Until n >= 9
        n = n + 1
        Debug.Print n
        If Cells(i, 15).Value = s3 And Cells(i, 13).Value = rA And Cells(i, 11) = "" Then
            Cells(i, 15).Interior.ColorIndex = 37
        End If
    Loop  
    Next i

我考虑过将n = 0放入循环中,但这只会打印1九千次,对吧?

你们能帮我解决这个问题以使其正常运行吗?我已经阅读了三天的循环教程,但无法正常工作:(非常感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

I've no idea if I'm reading your question correctly so this is a guess at your requirement.

The following reads all the lines (E) but keeps count of how many matches/changes in n and stops making those changes when n becomes higher than 9.

n = 0

For i = 1 To E
    If n < 10 And Cells(i, 15).Value = s3 And Cells(i, 13).Value = rA And Cells(i, 11) = "" Then
        Cells(i, 15).Interior.ColorIndex = 0
        n = n + 1
    End If
Next i

答案 1 :(得分:1)

根据您的实际操作,在Excel中循环行的行和列是一项标准任务。假设您的任务是在20个Excel列中生成一些数据,如下所示:

enter image description here

然后您的任务是遍历每行的每一列,并将其着色为红色(如果可以将其除以7):

enter image description here

然后,一个带有2个嵌套循环的解决方案可能是最简单的决定-一个循环用于行,每一行循环遍历各列:

Public Sub GenerateSomeData()

    Dim someRows As Long
    Dim someCols As Long

    Cells.Clear

    'GenerateSomeData
    For someRows = 1 To 100
        For someCols = 1 To 20
            Cells(someRows, someCols) = someRows + someCols
        Next someCols
    Next someRows

    'Search for values divisable by 7:
    For someRows = 1 To 100
        For someCols = 1 To 20
            If Cells(someRows, someCols) Mod 7 = 0 Then
                Cells(someRows, someCols).Interior.Color = vbRed
            End If
        Next someCols
    Next someRows

End Sub

答案 2 :(得分:0)

我想,我会做一些与CLR提供的答案非常相似的事情,但是会像下面这样稍作更改,因此您将经历i = 1 to E,但是一旦n = 9我就会退出循环为了节省资源:

n = 0

For i = 1 To E
    If Cells(i, 15).Value = s3 And Cells(i, 13).Value = rA And Cells(i, 11) = "" Then
        Cells(i, 15).Interior.ColorIndex = 37
        n = n + 1
    End If

    If n = 9 Then Exit For
Next i