Excel VBA:编译错误:找不到方法或数据成员 [英] Excel VBA: Compile Error: Method or data member not found
问题描述
编辑:为澄清起见,下面看到的代码在一个模块内,而UserForm都包含在其自己的代码内。
To clarify, the code seen below is within a module and the UserForm is all contained within its own code.
我有以下代码。当我运行它时,Excel抛出一个编译错误:未找到方法或数据成员
并突出显示以下代码: .showInputsDialog
。我不知道如何解决此错误。
I have the following code. When I go to run it, Excel throws me a compile error: Method or data member not found
and highlights the following piece of code: .showInputsDialog
. I have no idea how to resolve this error.
要提供更多信息,应该调用sub sportUserForm
创建一个用户窗体 sportsUsrFrm
。
To give more information, the sub sportUserForm
is supposed to call up a UserForm sportsUsrFrm
. Any help with this issue is greatly appreciated.
Option Explicit
Sub sportUserForm()
Dim sSport As String, sPreference As String
If sportsUsrFrm.showInputsDialog(sSport, sPreference) Then
MsgBox "Your favorite sport is " & sSport & ", and you usually " _
& sPreference & "."
Else
MsgBox "Sorry you don't want to play."
End If
End Sub
Public Function showInputsDialog(sSports As String, sPreference As String) As Boolean
Call Initialize
Me.Show
If Not cancel Then
If optBaseball.Value Then sSport = "Baseball"
ElseIf optBasketball.Value Then sSport = "Basketball"
Elss sSport = "Football"
End If
If optTV.Value Then sPreference = "watch on TV" _
Else: sPreference = "go to games"
End If
showInputsDialog = Not cancel
Unload Me
End Function
sportUsrFrm
Option Explicit
Private Sub cmdCnl_Click()
Me.Hide
cancel = True
End Sub
Private Sub cmdOK_Click()
If Valid Then Me.Hide
cancel = False
End Sub
推荐答案
您收到错误消息是因为 showInputsDialog
不是表单的成员,它是您从中调用它的模块的成员。您还应该在这两行上得到编译器错误...
You're getting the error because showInputsDialog
isn't a member of the form, it's a member of the module you're calling it from. You should also be getting compiler errors on these two lines...
Call Initialize
Me.Show
...因为您似乎混淆了模块和表单代码。
...because you seem to be getting the module and form code mixed up.
也就是说,您对此太想了。 UserForm是一个类模块,它可以存储在变量中(在这种情况下,可以存储在 With
块中),并且可以具有属性。我要在表单中添加一个 Cancelled
属性:
That said, you're overthinking this. A UserForm is a class module, and it can be stored in a variable (or in this case, in a With
block), and can have properties. I'd add a Cancelled
property to the form:
'In sportsUsrFrm
Option Explicit
Private mCancel As Boolean
Public Property Get Cancelled() As Boolean
Cancelled = mCancel
End Property
Private Sub cmdCnl_Click()
Me.Hide
mCancel = True
End Sub
Private Sub cmdOK_Click()
If Valid Then Me.Hide '<-- You still need to implement `Valid`
End Sub
然后这样称呼它:
Sub sportUserForm()
With New sportsUsrFrm
.Show
Dim sSport As String, sPreference As String
If Not .Cancelled Then
If .optBaseball.Value Then
sSport = "Baseball"
ElseIf .optBasketball.Value Then
sSport = "Basketball"
Else
sSport = "Football"
End If
If .optTV.Value Then
sPreference = "watch on TV"
Else
sPreference = "go to games"
End If
MsgBox "Your favorite sport is " & sSport & ", and you usually " _
& sPreference & "."
Else
MsgBox "Sorry you don't want to play."
End If
End With
End Sub
这篇关于Excel VBA:编译错误:找不到方法或数据成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!