Ok, so this one is raising my blood pressure. I have a report that checks the logistic route of a order like this
ElseIf LCase(rota) = "blumenau" Then
wkdas.Cells(86, 5) = wkdas.Cells(86, 5) + 1.2
wkdas.Cells(86, 6) = wkdas.Cells(86, 6) + 1
ElseIf LCase(rota) = "itajai" Or LCase(rota) = "joinville" Then
wkdas.Cells(87, 5) = wkdas.Cells(87, 5) + 1.2
wkdas.Cells(87, 6) = wkdas.Cells(87, 6) + 1
Now, first case there, it only has one logistic route city, and it works fine, the second one, with two logistic routes (that were treated as one logistic route in the report due to proximity but we have to keep as two routes in system for fiscal reasons), just. don’t. return. true.
I don’t know why. I went and inspected the variable rota as the code ran. It was "Itajai", went into the if statement with the LCase(rota) (Dim rota as String, by the way, I didn’t forget it). Returned as false.
I thought, maybe it’s some funky stuff with the Or there, right? So I did this:
ElseIf LCase(rota) = "blumenau" Then
wkdas.Cells(26, 5) = wkdas.Cells(26, 5) + 1.2
wkdas.Cells(26, 6) = wkdas.Cells(26, 6) + 1
ElseIf LCase(rota) = "itajai" Then
wkdas.Cells(27, 5) = wkdas.Cells(27, 5) + 1.2
wkdas.Cells(27, 6) = wkdas.Cells(27, 6) + 1
ElseIf LCase(rota) = "joinville" Then
wkdas.Cells(27, 5) = wkdas.Cells(27, 5) + 1.2
wkdas.Cells(27, 6) = wkdas.Cells(27, 6) + 1
It didn’t work. It works with "blumenau" and other cities, but aparently it has a bone to pick with "itajai" because it just won’t recognize the city. Does anyone has a better theory than "vba hates this one city"?
>Solution :
I would use Trim with Select Case to make your code easier to read and ignore leading and trailing spaces.
Select Case Trim(LCase(rota))
Case "blumenau"
wkdas.Cells(26, 5) = wkdas.Cells(26, 5) + 1.2
wkdas.Cells(26, 6) = wkdas.Cells(26, 6) + 1
Case "itajai", "joinville"
wkdas.Cells(27, 5) = wkdas.Cells(27, 5) + 1.2
wkdas.Cells(27, 6) = wkdas.Cells(27, 6) + 1
End Select