VBA功能,用于用户窗体中的控件 [英] VBA function for Controls in Userform
问题描述
我对VBA还是很陌生,所以我对这个问题看起来很傻表示歉意:我已经在其中建立了一个带有某些控件的UserForm,并按如下所示创建了一个名为ResetMyField
的函数:
I'm very new to VBA so I apologise if the question seems silly: I have set up a UserForm with some controls in it, and created a function called ResetMyField
as per below:
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
声明为其他类型的变量吗?
When I call this function using ResetMyField(ProjectReference)
VBA comes out with a 424 error (Object Required). Should I be declaring MyField
as a different type of variable in the function?
该函数及其调用点均位于Userform模块内部.
Both the function and the point at which I call it are inside the Userform module.
任何帮助将不胜感激.
推荐答案
ResetMyField(ProjectReference)
,带括号,尝试将ProjectReference
组合框的默认属性传递到ResetMyField
.组合框的默认属性为Value
,而不是Object
,并且ResetMyField
期望为Object
,因此会出现错误("Object Required").
ResetMyField(ProjectReference)
, with the parentheses, tries to pass the default property of the ProjectReference
combobox into ResetMyField
. The default property of a combobox is Value
, and that is not an Object
, and ResetMyField
expects an Object
, hence the error ("Object Required").
删除括号:
ResetMyField ProjectReference
还请注意,If MyField = ProjectReference Then
再次尝试比较MyField
和ProjectReference
的默认属性,如果使用组合框,则表示If MyField.Value = ProjectReference.Value Then
.
如果您想知道MyField
是 ProjectReference
,那么它是
Also note that If MyField = ProjectReference Then
, again, tries to compare default properties of MyField
and ProjectReference
, which in case of comboboxes will mean If MyField.Value = ProjectReference.Value Then
.
If you want to know if MyField
is ProjectReference
, then it's
If MyField Is ProjectReference Then
这篇关于VBA功能,用于用户窗体中的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!