尝试从另一个模块使用类方法时VBA错误424

时间:2015-04-10 12:53:13

标签: excel vba class methods

我在excel 2013的类模块中有一个名为autoCRUD的类。从另一个模块(常规模块)开始,我尝试从这个类调用一个方法,然后得到"对象需要"异常。

以下是方法:

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应该是一个字符串值,即标签的名称。它来自表格标题中的电子表格,teh legenda()只选择那个标题,而arrLeg将legenda作为一个范围并将其转换为数组。 编辑:

显然,错误发生在以下行中:.name = "lbl_" + Replace(CStr(lbl.Value), " ", "") 正如你所看到的,我试图从字符串中取出空格并确保它是一个字符串,但它都没有用。

编辑2:

我实际上只是将一个类用于组织和可重用性目的。我采用属性和其他方法,并在&#39; createCRUDView&#39;中使用它们。方法,这个方法然后将创建一个CRUD视图,即创建一个表单以及#34;创建&#34;,&#34;读取&#34; (因为它超越而未使用),&#34;更新或&#34;删除&#34;数据条目。它基本上为您制作的任何表格动态创建表单

1 个答案:

答案 0 :(得分:1)

VBA错误424是对象要求错误。所以我现在非常确定lbl中的CStr(lbl.Value)不是对象。您的代码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”表格是ActiveSheet时才起作用,因为Cells(linha, colunaI)未明确分配给工作表,因此可以使用ActiveSheet。

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

会更好。