Excel VBA:编译错误:找不到方法或数据成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40251064/
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-08 10:36:13  来源:igfitidea点击:

Excel VBA: Compile Error: Method or data member not found

excelvbaexcel-vbacompiler-errors

提问by Jonathan Rauscher

EDIT: To clarify, the code seen below is within a module and the UserForm is all contained within its own code.

编辑:澄清一下,下面看到的代码在一个模块中,用户窗体都包含在它自己的代码中。

I have the following code. When I go to run it, Excel throws me a compile error: Method or data member not foundand highlights the following piece of code: .showInputsDialog. I have no idea how to resolve this error.

我有以下代码。当我去运行它时,Excel 向我抛出一个编译错误:Method or data member not found并突出显示以下代码段:.showInputsDialog. 我不知道如何解决这个错误。

To give more information, the sub sportUserFormis supposed to call up a UserForm sportsUsrFrm. Any help with this issue is greatly appreciated.

为了提供更多信息, subsportUserForm应该调用 UserForm sportsUsrFrm。非常感谢您对此问题的任何帮助。

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

UserForm code for sportUsrFrm

用户表单代码 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

UserForm image

用户窗体图像

回答by Comintern

You're getting the error because showInputsDialogisn'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...

您收到错误是因为showInputsDialog它不是表单的成员,而是您从中调用它的模块的成员。您还应该在这两行上收到编译器错误...

Call Initialize
Me.Show

...because you seem to be getting the module and form code mixed up.

...因为您似乎将模块和表单代码混淆了。

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 Withblock), and can have properties. I'd add a Cancelledproperty to the form:

就是说,你想多了。UserForm 是一个类模块,它可以存储在一个变量中(或者在这种情况下,在一个With块中),并且可以具有属性。我会Cancelled在表单中添加一个属性:

'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

And then call it like this:

然后像这样调用它:

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