2017-07-03 48 views
0

我正在尝试执行多条件搜索。如果两个参数等于另一个工作表中的另外两个参数,则执行一些操作。嵌套做虽然内循环只在外循环的第一次迭代时运行

内部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

'j'需要重置为'0'后/内环否则第二次轮前的值不符合“while”条件。 –

回答

2

费德里科,你需要重置每次j你环路外环:

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

非常感谢!剩下的只剩下一点,没有看到那个啊 –

+0

不客气。容易忽视的事情。每当一个do-while循环只能运行**一次**这是一件需要注意的事情。 – TomServo