【问题标题】:Excel VBA: Compile Error: Method or data member not foundExcel VBA:编译错误:找不到方法或数据成员
【发布时间】:2016-10-25 22:56:26
【问题描述】:

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

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

为了提供更多信息,子sportUserForm 应该调用用户窗体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

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

【问题讨论】:

  • 什么代码在什么模块里?一切都在用户表单中吗?我建议查看文档中的this example,以获得更好的表单处理方法。
  • @Comintern 我已经编辑了 OP 以反映您的问题。
  • 可以添加sportsUsrFrm_Initialize的代码吗?
  • @Comintern 除非这是默认添加的代码,否则我不相信我已经为sportsUsrFrm_Initialize 编写了任何代码。
  • 用户窗体中是否有任何代码?

标签: vba excel compiler-errors


【解决方案1】:

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

Call Initialize
Me.Show

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

也就是说,你想多了。 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

然后这样称呼它:

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

【讨论】:

    猜你喜欢
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多