嵌套Do虽然内部循环仅在外循环的第一次迭代时运行

时间:2017-07-03 14:30:16

标签: excel vba excel-vba

我正在尝试进行多项条件搜索。如果两个参数等于另一个工作表中的另外两个参数,那么就做一些事情。

内部Do While运行一次。

Sub main()

Dim mat As String
Dim sp As String

Dim colArt As Integer
colArt = 8

Dim colMat As Integer
colMat = 2

Sheets("Art.1 (2)").Activate
Dim i As Integer
i = 5

Dim j As Integer
j = 2 'Row
Do While i < 12
    If (Cells(i, colArt) <> "") Then

        Dim tempMat As String
        tempMat = Cells(i, colArt)

        Dim tempSp As Integer
        tempSp = Cells(i, colArt - 1)

        Do While j < 16
            If (Worksheets("Mat").Cells(j, colMat) <> "") Then

                Dim tempMatMat As String
                tempMatMat = Worksheets("Mat").Cells(j, colMat)

                If (StrComp(tempMat, tempMatMat, vbTextCompare) = 0) Then
                    MsgBox (tempMatMat)
                End If

            End If
        j = j + 1
        Loop

    End If
i = i + 1
Loop
End Sub

1 个答案:

答案 0 :(得分:2)

Federico,每次循环外循环时都需要重置j

Do While i < 12
    j = 2
    If (Cells(i, colArt) <> "") Then
        Dim tempMat As String
        tempMat = Cells(i, colArt)