2

I am trying to compare 2 string values at the same time - Account # and Account Name. I've explicitly set all variables as Strings, however I am still getting a mismatch error in my IF comparison.

Does anyone know what is it I'm doing wrong?


Dim i As Integer
For i = 2 To 10

    Dim accountNumber As String
    accountNumber = CStr(Sheet1.Cells(i, 2).Value)
    Dim accountName As String
    accountName = CStr(Sheet1.Cells(i, 3).Value)
    Dim c As Integer
    c = 4

    Dim j As Integer
    For j = 2 To 14831
        Dim accountNumber2 As String
        accountNumber2 = CStr(Sheet2.Cells(j, 2).Value)
        Dim accountName2 As String
        accountName2 = CStr(Sheet2.Cells(j, 3).Value)

        If (accountNumber = accountNumber2 & accountName = accountName2) Then
            Dim platform As String
            platform = Sheet2.Cells(j, 11).Value
            Sheet1.Cells(i, c).Value = platform
            c = c + 1
        End If
    Next j

Next i
End Sub
2
  • 1
    & is not and in VBA. Try: If accountNumber = accountNumber2 and accountName = accountName2 Then Commented Nov 28, 2019 at 22:45
  • Hi Jerry. If my answer worked for you, please mark it as the accepted answer to let others know it worked. Thanks. Commented Nov 29, 2019 at 21:44

1 Answer 1

2

As @cybernetic.nomad writes in a comment below your question, you should be using the and operator which is logical and instead of & which for strings means concatenation.

When you write

accountNumber = accountNumber2 & accountName = accountName2

then & has higher precedence than = so effectively this is what it means:

accountNumber = (accountNumber2 & accountName) = accountName2

So what happens is:

  1. You are first concatenating accountNumber2 and accountName resulting in a string.
  2. You are then comparing accountNumber to the result of the concatenation resulting in a boolean value.
  3. Finally, you are trying to compare that boolean to accountName2 which is a string and this is probably not what you want.
Sign up to request clarification or add additional context in comments.

Comments

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.