Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a Sub in VBA

This my simplified script:

    Sub SomeOtherSub(Stattyp As String)         'Daty and the other variables are defined here          CatSubProduktAreakum(Stattyp, Daty + UBound(SubCategories) + 2)      End Sub      Sub CatSubProduktAreakum(Stattyp As String, starty As Integer)      'some stuff      End Sub 

The call of CatSubProduktAreakum is marked red as a "syntax error". I don't understand the error. It is a simple sub-routine call with two arguments. Why does VBA not accept the call?

like image 522
reggie Avatar asked Oct 10 '11 15:10

reggie


People also ask

How do you call a sub function in VBA?

To call a Sub procedure from another procedure, type the name of the procedure and include values for any required arguments. The Call statement is not required, but if you use it, you must enclose any arguments in parentheses. Use a Sub procedure to organize other procedures so they are easier to understand and debug.

How do you call a sub in Visual Basic?

Calling a Sub Procedure You call a Sub procedure by using the procedure name in a statement and then following that name with its argument list in parentheses. You can omit the parentheses only if you don't supply any arguments. However, your code is more readable if you always include the parentheses.

Can we call private sub in VBA?

When we think about Private subs, it is best to view them as VBA code which can only be called by other VBA code within the same module. For example, if Module1 contains a Private Sub, it cannot be called by any code in another module.


2 Answers

Try -

Call CatSubProduktAreakum(Stattyp, Daty + UBound(SubCategories) + 2) 

As for the reason, this from MSDN via this question - What does the Call keyword do in VB6?

You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.

like image 151
ipr101 Avatar answered Sep 21 '22 03:09

ipr101


For anyone still coming to this post, the other option is to simply omit the parentheses:

Sub SomeOtherSub(Stattyp As String)     'Daty and the other variables are defined here      CatSubProduktAreakum Stattyp, Daty + UBound(SubCategories) + 2  End Sub 

The Call keywords is only really in VBA for backwards compatibilty and isn't actually required.

If however, you decide to use the Call keyword, then you have to change your syntax to suit.

'// With Call Call Foo(Bar)  '// Without Call Foo Bar 

Both will do exactly the same thing.


That being said, there may be instances to watch out for where using parentheses unnecessarily will cause things to be evaluated where you didn't intend them to be (as parentheses do this in VBA) so with that in mind the better option is probably to omit the Call keyword and the parentheses

like image 20
SierraOscar Avatar answered Sep 24 '22 03:09

SierraOscar