0

I want to call a Sub I declared at it gives a compilation error saying that it expects a =. The Sub call is in a UserForm_Initialize event procedure. The code is as follows.

In a module:

Public Sub FillCb(Ar() As String, Cb As ComboBox)
    Cb.Clear
    For I = 1 To Application.CountA(Ar)
        Cb.AddItem (Ar(I))
    Next I
End Sub

In the UserForm code:

Private Sub UserForm_Initialize()
    LblDate.Caption = Date
    FillCb(LibrosNoPrestados, CbLibro)
End Sub

This code is giving me error. I analized the code line by line using the debugger and commenting the las line inside the Initialize event, and it works fine up to that point. The error is thrown at compile time in the

FillCb(LibrosNoPrestados, CbLibro)

The rest of the code is not needed here since as I said it works fine, but the syntax in that last line must be wrong and I can't see the mistake.

Lorthas
  • 376
  • 3
  • 11

1 Answers1

1

A VBA "feature". If you are calling a sub routine without the "Call" keyword then don't use parentheses, if you use the "Call" keyword then you need the parentheses.

Eg Call FillCb(LibrosNoPrestados, CbLibro)

Or

FillCb LibrosNoPrestados, CbLibro

Here's Microsoft's documentation: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/call-statement

ACCtionMan
  • 511
  • 1
  • 3
  • 12
  • Oops. I used stock functions using the correct syntax and didn't realize! Thank you very much! – Lorthas Mar 17 '20 at 17:03
  • *A VBA "feature" - no, it's not a matter of VBA's possible *misbehaviour*, it's a matter of understanding how VBA treats procedure arguments. C.f. Mathieu Guindon's comment to [VBA Runtime error ...](https://stackoverflow.com/questions/44926523/excel-vba-runtime-error-424-object-required-when-assigning-range-variable/47269636#47269636) or [this](https://stackoverflow.com/questions/59775179/why-only-one-of-these-2-smilar-macros-to-create-bordures-works/59775502#59775502) – T.M. Mar 17 '20 at 17:13