Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Call keyword in VB/VBA?

Tags:

vb.net

vba

call

vb6

I use the Call keyword when calling subs in VB/VBA. I know it's optional, but is it better to use it or leave it off? I've always thought it was more explicit, but maybe it's just noise.

Also, I read this on another forum: Using the Call keyword is faster because it knows that it is not going to return any values, so it doesn't need to set up any stackspace to make room for the return value.

like image 331
Fred Loyant Avatar asked Apr 04 '10 04:04

Fred Loyant


People also ask

What does the call statement do in VBA?

Transfers control to a Sub procedure, Function procedure, or dynamic-link library (DLL) procedure.

Why is the call keyword used?

You typically use the Call keyword when the called expression doesn't start with an identifier. Use of the Call keyword for other uses isn't recommended. If the procedure returns a value, the Call statement discards it.

How do you call in VBA?

Just type the word Call then space, then type the name of the macro to be called (run). The example below shows how to call Macro2 from Macro1. It's important to note that the two macros DO NOT run at the same time. Once the Call line is hit, Macro2 will be run completely to the end.

How do you call a function in a VBA module?

Step 1: Open a New Excel workbook. Step 2: Press Alt+F11 – This will open the VBA Editor (alternatively, you can open it from Developer Tab in Excel Ribbon) Step 3: Insert a code module from then insert menu of the VBE. Step 4: Copy the above code and paste in the code module which have inserted in the above step.


2 Answers

Ah ha. I have long wondered about this and even reading a two inch thick book on VBA basically says don't use it unless you want to use the Find feature of the VBE to easily find calls in large projects.

But I just found another use.

We know that it's possible to concatenate lines of code with the colon character, for example:

Function Test(mode as Boolean)      if mode = True then x = x + 1 : Exit Sub     y = y - 1 End Sub 

But if you do this with procedure calls at the beginning of a line, the VBE assumes that you're referring to a label and removes any indents, aligning the line to the left margin (even though the procedure is called as intended):

Function Test() Function1 : Function2 End Function 

Using the Call statement allows concatenation of procedure calls while maintaining your code indents:

Function Test()     Call Function1 : Call Function2 End Function 

If you don't use the Call statement in the above example, the VBE will assume that "Function1" is an label and left align it in the code window, even though it won't cause an error.

like image 155
Jamie Garroch - MVP Avatar answered Sep 21 '22 10:09

Jamie Garroch - MVP


For VB6, if there is any chance it will be converted to VB.NET, using Call means the syntax doesn't change. (Parentheses are required in VB.NET for method calls.) (I don't personally think this is worth the bother -- any .NET converter will at least be able to put in parentheses when required. I'm just listing it as a reason.)

Otherwise it is just syntactic sugar.

Note the Call keyword is likely not to be faster when calling some other method/function because a function returns its value anyway, and VB didn't need to create a local variable to receive it, even when Call is not used.

like image 23
Mark Hurd Avatar answered Sep 18 '22 10:09

Mark Hurd