Excel VBA object sub call with 2 object parameters gives compile error: expected =

匿名 (未验证) 提交于 2019-12-03 01:40:02

问题:

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?

回答1:

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

Use it like this

employee.loadFromAnotherWorkbook eswb, wbcolumns 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!