带有2个对象参数的Excel VBA对象子调用给出了编译错误:expected = [英] Excel VBA object sub call with 2 object parameters gives compile error: expected =

查看:220
本文介绍了带有2个对象参数的Excel VBA对象子调用给出了编译错误:expected =的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在微软excel vba中调用一个对象的子例程。子有两个参数,两个对象本身。我输入的代码行给出错误 - >编译错误:Expected =



以下是代码段:

 '复制旧表格中的信息并粘贴到新工作表
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) < - 这行给出编译错误

下一个I

我不明白为什么会这样。这个代码类似于我已经可以正常工作的代码了。



此代码工作正常(注意:这是一个单独的函数):

  With ThisWorkbook.Worksheets(sheet)
Do While(.Cells(i,1).value<> Empty)
'创建对象并设置名称属性
设置employee =新建CEmployee
employee.setNames(.Cells(i,1).value)

employee.loadFromScratch'< - 这个sub几乎与导致问题的sub相同。唯一的区别是它不采取对象参数

i = i + 1
循环
结束

这是我如何声明我调用的子例程给出了编译错误:

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

对象我传入这个sub是正确的类型。



这不是一个功能,所以我不明白为什么我需要使用等号。任何人都知道我做错了什么?

解决方案

当调用 Sub 你不用括号括起来参数



像这样使用

  employee.loadFromAnotherWorkbook eswb,wbcolumns 


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?

解决方案

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

Use it like this

employee.loadFromAnotherWorkbook eswb, wbcolumns

这篇关于带有2个对象参数的Excel VBA对象子调用给出了编译错误:expected =的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆