0

I am trying to do a multiple condition search. If two parameters are equal to another two parameters in another worksheet, then do something.

The inner Do While runs once.

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
  • 1
    j needs resetting to 0 after/before the inner loop else the 2nd time round its value does not satisfy the while condition. Commented Jul 3, 2017 at 14:31

1 Answer 1

2

Federico, you need to reset j each time you loop in the outer loop:

Do While i < 12
    j = 2
    If (Cells(i, colArt) <> "") Then
        Dim tempMat As String
        tempMat = Cells(i, colArt)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! Little left over, didnt see that ahah
You're welcome. Easy thing to overlook. Whenever an do-while loop only works once this is a thing to look out for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.