String Comparison Bug ???

russdb

New Member
Joined
Jun 18, 2012
Messages
1
I'm having a problem comparing strings. The code below works on one machine and does NOT on the other. The only difference is the version of Excel. I'm using 2010 and my buddies using 2003.

Can I hear back from someone who had a similar issue???

Sub main()

For i1 = 1 To 1000

For i2 = 1 To 1000

If Worksheets("Sheet1").Cells(i1, 1).Value = Worksheets("Sheet2").Cells(i2, 1).Value And _
Worksheets("Sheet1").Cells(i1, 2).Value = Worksheets("Sheet2").Cells(i2, 2).Value Then

Worksheets("Sheet1").Cells(i1, 3).Value = Worksheets("Sheet2").Cells(i2, 3).Value
Worksheets("Sheet1").Cells(i1, 4).Value = Worksheets("Sheet2").Cells(i2, 4).Value

Exit For

End If

Next

Next

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi and Welcome to the Board,

There's much more efficient ways to do this task.
You can find lots of examples on this Site or the Web looking with keywords like: Vlookup with Multiple Criteria.

If you still want to figure out why this works on one machine and not another, you might want to use a small sample (maybe 3 items on each sheet) and print the results of each comparison to the Immediate Window in the VB Editor. If the Immediate Window isn't already displayed enter (Ctrl-G) to show it.

Code:
Sub main()
    Dim i1 As Long, i2 As Long
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
            
    For i1 = 1 To 3
        For i2 = 1 To 3
            
            Debug.Print "i1/i2: " & i1 & "/" & i2 & " >>> " & ws1.Cells(i1, 1) _
                & "=" & ws2.Cells(i2, 1) & "? Result1: " _
                & (ws1.Cells(i1, 1) = ws2.Cells(i2, 1))
            Debug.Print "       " & i1 & "/" & i2 & " >>> " & ws1.Cells(i1, 2) _
                & "=" & ws2.Cells(i2, 2) & "? Result2: " _
                & (ws1.Cells(i1, 2) = ws2.Cells(i2, 2))
               
            If ws1.Cells(i1, 1) = ws2.Cells(i2, 1) And _
                ws1.Cells(i1, 2) = ws2.Cells(i2, 2) Then
                    Debug.Print "2-Criteria Match found"
                    
                    ws1.Cells(i1, 3) = ws2.Cells(i2, 3)
                    ws1.Cells(i1, 4) = ws2.Cells(i2, 4)
                    Exit For
            Else
                Debug.Print "  No Match"
            End If
         Next
    Next
End Sub

Hopefully the Immediate Window output will make the cause of the unexpected results apparent.
If it still appears that one of the computers has a string comparison bug, please describe your findings from this testing.
 
Upvote 0

Forum statistics

Threads
1,203,380
Messages
6,055,099
Members
444,762
Latest member
MRC3411

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top