Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA equivalent to SQL 'in' function

Tags:

vba

I'm writing a conditional statement in vba like

if(userID = 1 or userID = 2 or userID = 3 or userID = 4) then
...

I was wondering if there's a quicker, cleaner way to do this. Something like

if(userID in (1,2,3,4)) then
...

Thanks

like image 455
Ben Avatar asked Jul 09 '13 16:07

Ben


People also ask

What is SQL in VBA?

VBA Code and SQL SQL stands for Structured Query Language and is the language used to extract data from almost all databases like Access and SQL Server from Microsoft or, Oracle, Sybase, SAP and also most accounting applications.

Is VBA and SQL similar?

While Visual Basic (VBA is an implementation of Visual Basic) is a general-purpose scripting programming language, SQL is a special-purpose programming language- aimed at running queries and CRUD (Create, Read, Update, Delete) operations on relational databases such as MySQL, MS SQL, Oracle, MS Access etc.

Is there an AND function in Excel VBA?

The AND function is a built-in function in Excel that is categorized as a Logical Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.

Can you call a function in VBA?

If you are not interested in the return value of a function, you can call a function the same way you call a Sub procedure. Omit the parentheses, list the arguments, and don't assign the function to a variable, as shown in the following example.


1 Answers

An alternative would be:

select case userID
    case 1,2,3,4,5,6
       ' do something
end select

It conveys very good the meaning of the if ... then ... else construct.

like image 140
MicSim Avatar answered Oct 10 '22 00:10

MicSim