VBA 尝试使用来自另一个模块的 class 方法时出现错误 424

VBA error 424 when trying to use class method from another module

我在 excel 2013 年的 class 模块中有一个名为 autoCRUD 的 class。从另一个模块(常规模块)我尝试从这个 class 然后我得到 "Object required" 异常。

方法如下:

Public Function CreateCRUDView(TipoCRUD As String) 'TipoCRUD pode ser C (Create), R (Read), U (Update), D (Delete)
    Dim myForm As Object
    Dim NewFrame As MSForms.Frame
    Dim NewButton As MSForms.CommandButton
    Dim NewListBox As MSForms.ListBox
    Dim NewLabel As MSForms.Label
    Dim X As Integer
    Dim Line As Integer
    Dim t As Integer

    Dim arrLeg() As Variant
    arrLeg = legenda

    'This is to stop screen flashing while creating form
    Application.VBE.MainWindow.Visible = False

    Set myForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)


    'Create the User Form
    With myForm
        .Properties("Caption") = "New Form"
        .Properties("Width") = 300
        .Properties("Height") = 270
    End With

    'Criar labels
    t = 10
    For Each lbl In arrLeg
        Set NewLabel = myForm.Designer.Controls.Add("Forms.label.1")
        With NewLabel
            .name = "lbl_" + Replace(CStr(lbl.Value), " ", "")
            .t = (10 + t)
            .Left = 10
            .Font.Size = 8

        End With
        t = t + 10
    Next

        'Create CommandButton Create
        Set NewButton = myForm.Designer.Controls.Add("Forms.commandbutton.1")
        With NewButton
            .name = "cmd_1"
            If UCase(TipoCRUD) = "C" Then
                .Caption = "Salvar"
            ElseIf UCase(TipoCRUD) = "U" Then
                .Caption = "Alterar"
            End If
            .Accelerator = "M"
            .Top = Top + 10
            .Left = 200
            .Width = 66
            .Height = 20
            .Font.Size = 8
            .Font.name = "Tahoma"
            .BackStyle = fmBackStyleOpaque
        End With
        Top = Top + 10
        End Function

调用该方法的另一个模块的代码是:

Public Sub Main()

       Dim ac As autoCrud
       Set ac = New autoCrud

       ac.CreateCRUDView ("c")

End Sub

我不明白,为什么会出现此错误?

这是 "legenda" 的代码:

Public Property Get sht() As Worksheet
    Const shtName As String = "Teste1"
    Set sht = ActiveWorkbook.Worksheets(shtName)
End Property
Public Property Get legenda() As Range
    Const linha As Integer = 3
    Const colunaI As Integer = 2
    Dim colunaF As Integer
    Dim i As Integer
    i = colunaI
    Do While sht.Cells(linha, i).Value <> ""
        i = i + 1
    Loop
    colunaF = (i - 1)
    Set legenda = sht.Range(Cells(linha, colunaI), Cells(linha, colunaF))
End Property

lbl.Value应该是一个字符串值,标签的名称。而它来自table的header中的spread sheet,teh legenda()只选择了header并且arrLeg将legenda作为范围并对其进行转换在一个数组中。 编辑:

显然错误发生在以下行中:.name = "lbl_" + Replace(CStr(lbl.Value), " ", "") 如您所见,我尝试从字符串中取出空格并确保它是一个字符串,但是 none 它起作用了。

编辑 2:

我实际上只是使用 class 进行组织和 re-usability 目的。我获取属性和其他方法并在 'createCRUDView' 方法中使用它们,然后此方法将创建一个 CRUD 视图,即创建一个表单到 "Create"、"Read"(未使用因为它是 excel),"Update or "Delete" 数据条目。它基本上为您创建的任何 table 动态创建表单

VBA 错误 424 是对象必需的错误。所以我现在很确定 CStr(lbl.Value) 中的 lbl 不是一个对象。使用您的代码 legendaRange 但在

之后
Dim arrLeg() As Variant
arrLeg = legenda

arrLeg 将是一个变体数组。该数组不包含对象。您可以使用

进行调试
For Each lbl In arrLeg
 ...
 MsgBox TypeName(lbl)
 ...
Next

所以你应该使用CStr(lbl)

Set legenda = sht.Range(Cells(linha, colunaI), Cells(linha, colunaF))

仅当 "Teste1" sheet 是 ActiveSheet 时才有效,因为 Cells(linha, colunaI) 未明确分配给 sheet,因此应该是 ActiveSheet。

Set legenda = sht.Range(sht.Cells(linha, colunaI), sht.Cells(linha, colunaF))

会更好。