Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any limitation for IF condition while using "OR" in vb?

Tags:

vba

vb6

vbscript

In my code i want to use if condition. In which i want to use "OR" around 18 times. Like for e.g.

If a="something" or a="something" or a="something" or.........(up to 18 times)... then
  'do nothing
else
  'do action
end if

[Note : value of a is changing in For loop every time] so i just want to ask does there any limitation in IF for using OR in limited times. OR is there any other better way to do the same.

Thanks

like image 230
Pratik Gujarathi Avatar asked Feb 06 '12 13:02

Pratik Gujarathi


People also ask

How do you break a line in Visual Basic?

Use the line-continuation character, which is an underscore ( _ ), at the point at which you want the line to break. The underscore must be immediately preceded by a space and immediately followed by a line terminator (carriage return) or (starting with version 16.0) a comment followed by a carriage return.

How do I wrap a VBA code?

Wrap Text to a Cell using VBADefine the cell where you want to apply the wrap text using the range property. Type a dot to see the list of the properties and methods for that cell. Select the “WrapText” property from the list. Enter the equals sign “=” and the type TRUE to turn the wrap text ON.


2 Answers

As far as I know, there is no limitation when using OR this way.

Yet, you may consider alternative ways of coding this.

Negating a condition using Not

First, if you do nothing in the first case, then consider using the Not statement:

If Not True Then
'do somethin
'no else
End If

Consider using Select Case

Second, if you are checking the very same variable, you could either consider using a Select Case but it doesn't seem appropriate in your case if you have only one case.

Try to use a search

Eventually, if you are checking strings, you could probably better use a search within an array (with Application.Match if you are within Excel or .Contains) or within a String using Instr.

Using a collection or a dictionary

[EDIT] Another very good way to handle this would be to use the Dictionary Structure of VBA and check if a exists (see MSDN for some information).

like image 134
JMax Avatar answered Nov 13 '22 04:11

JMax


This answer is just an elaboration on my comments to JMay, full credit to him. I think the original poster meant to the "Something" in his question differ, with a being the loop variable.

For each a in MyList

   Select Case a
   Case "something", "something2", "something3", "something4", "something5", _
        "something6", "something7", "something8", "something9", "something10", _
        "something11", "something12", "something13", "something14", "something15", _
        "something16", "something17", "something18"

       'DO NOTHING

   Case Else

       'do-something code goes here
       ' and here
       ' and here
       ' and here
   End Select

Next a
like image 45
tcarvin Avatar answered Nov 13 '22 05:11

tcarvin