samedi 22 décembre 2018

VBA, avoid repeating AND, inside an IF statement

I need some help with editing a VBA code. What I am trying to do is to avoid repeating AND inside an IF statement.

So far I have this, and it works like a charm.

If Range("B1") <> "MatDescr" And Range("B2") <> "MatDescr" And Range("B3") <> "MatDescr" Then
MsgBox "Coloumn 'B' does not contain MatDescr"
...rest of the code...

So, as mentioned above I want to avoid using so many ANDs inside the code as I will have to write this in some other codes as well. What currently came to my mind, was to shorten the code by writing this:

Dim s As Integer 
For s = 1 To 3
If Cells(s, 2) <> "MatDescr" Then
next s 
rest of the code

This looks much better, but in this way, VBA sees that statement as I am trying to use OR, instead of AND. Which is not what I am aiming for.

Aucun commentaire:

Enregistrer un commentaire