0

I am calling an object's subroutine in Microsoft excel vba. The sub has 2 parameters, both objects themselves. The line of code I typed is giving an error-> Compile Error: Expected =

Here is the section of code that it occurs in:

' Copy the info from the old sheet and paste into the new sheet
Dim employee as CEmployee
For i = 2 To eswbMaxRow
    Set employee = New CEmployee
    employee.setNames (eswb.Worksheets("Employee Info").Cells(i, wbColumns.infoNameCol).value)
    employee.loadFromAnotherWorkbook(eswb,wbcolumns)   ' <- This line is giving the compile error

Next I

I don't understand why this is. This code is similar to code I already have that works fine.

This code works perfectly (Note: this is a separate function):

With ThisWorkbook.Worksheets(sheet)
   Do While (.Cells(i, 1).value <> Empty)
       ' Create an object and set the name property
       Set employee = New CEmployee
       employee.setNames (.Cells(i, 1).value)

       employee.loadFromScratch   ' <- This sub is nearly identical to the one that is causing the problem.  The only difference is it doesn't take object parameters

       i = i + 1
   Loop
End With

This is how I am declaring the subroutine that I am calling that gives the compile error:

Public Sub loadFromAnotherWorkbook(ByVal wb As Workbook, ByVal wbColumns As CColumns)

The objects I pass into this sub are of the correct type.

This isn't a function, so I don't understand why I would need to use an equal sign. Anyone know what I am doing wrong?

Community
  • 1
  • 1
Jason247
  • 974
  • 4
  • 16
  • 38
  • possible duplicate of [VBA returning error when calling a Sub with multiple parameters](http://stackoverflow.com/questions/13624279/vba-returning-error-when-calling-a-sub-with-multiple-parameters) – brettdj Apr 10 '14 at 07:29

1 Answers1

3

When calling a Sub, you don't enclose the parameters in brackets

Use it like this

employee.loadFromAnotherWorkbook eswb, wbcolumns
chris neilsen
  • 52,446
  • 10
  • 84
  • 123
  • Thanks! That got rid of the error. It's strange though because I've always enclosed sub parameters in brackets and I've never had a problem before. – Jason247 Apr 10 '14 at 07:05
  • @jason247 you can use brackets when calling a sub if you use `Call` - see the link I posted above where the same question was asked. – brettdj Apr 10 '14 at 07:35
  • 1
    You can also use `Call MySub(params, params...)` – ttaaoossuuuu Apr 10 '14 at 07:36