44

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?

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
reggie
  • 3,523
  • 14
  • 62
  • 97

2 Answers2

69

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.

Community
  • 1
  • 1
ipr101
  • 24,096
  • 8
  • 59
  • 61
  • 2
    Thanks, that seems to work. If I call the Sub-routine with only one argument (and define it with only one argument), then I can call it just by its name (and the argument in parenthesis). Why is it not possible to just call a Sub with two arguments? – reggie Oct 10 '11 at 15:28
  • 3
    @reggie You can call a sub without Call as long as you skip the brackets `Mysub arg1,arg2,arg3` However, you need to read up on ByVal and ByRef. – Fionnuala Oct 10 '11 at 15:31
  • ... for example http://stackoverflow.com/questions/1070863/hidden-features-of-vba – Fionnuala Oct 10 '11 at 15:36
  • 3
    and this is why i hate VBA. – Josh O'Bryan Jul 17 '13 at 22:44
13

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

SierraOscar
  • 17,507
  • 6
  • 40
  • 68