Is there any restriction for the IF condition when using "OR" in vb?

In my code I want to use if condition. In which I want to use "OR" about 18 times. Such as,

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

[Note: the value of a changes to Per cycle every time] so I just want to ask if there are any restrictions in IF for using OR for a limited time. OR is there any other better way to do the same.

thanks

+4
source share
2 answers

As far as I know, there are no restrictions when using OR .

However, you can consider alternative ways to code this.

Negation of condition using parameter

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

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

Consider Using Select Case

Secondly, if you are checking the same variable, you can consider using Select Case , but in your case this does not seem to be appropriate if you have only one case.

Try using search

After all, if you are checking strings, it might be better to use an array search (with Application.Match if you are in Excel or .Contains ) or inside a string using Instr .

Using a collection or dictionary

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

+14
source

This answer is just a study of my comments on JMay, full credit for him. I think that the original poster, designed for β€œsomething” in his question, is different, with a being a 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 
+6
source

Source: https://habr.com/ru/post/1395010/


All Articles