Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If/or not returning true

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading