我对 VBA 很陌生,所以如果这个问题看起来很愚蠢,我深表歉意:我已经设置了一个包含一些控件的用户窗体,并创建了一个名为 ResetMyField
的函数如下所示:
Function ResetMyField(MyField As Object)
If MyField = ProjectReference Then
'do something different and then
End If
MyField.Value = ""
End Function
当我使用
ResetMyField(ProjectReference)
调用此函数时VBA 出现 424 错误(需要对象)。我应该声明 MyField
作为函数中不同类型的变量?函数和我调用它的点都在 Userform 模块中。
任何帮助将非常感激。
最佳答案
ResetMyField(ProjectReference)
, with the parentheses , 试图通过 default property的ProjectReference
组合框到 ResetMyField
.组合框的默认属性是 Value
,这不是 Object
, 和 ResetMyField
预计 Object
,因此出现错误(“需要对象”)。
Remove the parentheses :
ResetMyField ProjectReference
另请注意
If MyField = ProjectReference Then
,再次尝试比较 MyField
的默认属性和 ProjectReference
,如果是组合框,则表示 If MyField.Value = ProjectReference.Value Then
.如果你想知道
MyField
is ProjectReference
,那么就是If MyField Is ProjectReference Then
关于excel - 用户窗体中控件的 VBA 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53481500/