ByRef 参数与 Excel 中的 VBA 不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10986528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:27:30  来源:igfitidea点击:

ByRef Argument Mismatch with VBA in Excel

excelvbavariablesmethodsparameters

提问by Ehudz

I have a Let property defined as:

我有一个 Let 属性定义为:

Public Property Let Set_ChanArray_Enabled1(i As Integer, j As Integer, choice As Boolean)
    ChanArray(i, j).Enabled1 = choice
End Property

In a sub defined in the same object module, I attempt to do the following:

在同一个对象模块中定义的子中,我尝试执行以下操作:

For j = 4 To 44
    Me.Set_ChanArray_Enabled1(j, 1) = True
    Me.Set_ChanArray_Enabled1(j, 3) = True
Next j 

But VBE gives me a ByRef argument mismatch pointing to the j passed into

但是 VBE 给了我一个 ByRef 参数不匹配,指向传入的 j

Me.Set_ChanArray_Enabled1(j, 1) = True

I have defined both j and the parameter passed into the method as integers so I am not sure what is wrong.

我已经将 j 和传递给方法的参数都定义为整数,所以我不确定有什么问题。

回答by Alex K.

That error indicates something is wrong with the typing of j(i.e its not of type integer).

该错误表明输入有问题j(即它不是整数类型)。

Have you declared it in a statement like; dim j, i as integer? If so then only iis an integer (you need to repeat as integer).

你有没有在这样的声明中声明它;dim j, i as integer? 如果是这样,那么只有i一个整数(你需要重复as integer)。

(Using byvalappears to "fix" this because its pass-by-copy semantics allow VBA to perform an automatic type conversion to integer before calling Set_ChanArray_Enabled1).

(使用byval似乎“修复”了这个问题,因为它的通过复制语义允许 VBA 在调用之前执行到整数的自动类型转换Set_ChanArray_Enabled1)。