Home > Enterprise >  VBA ClassModule in Excel doesn't recognise all methods (run-time error '438')
VBA ClassModule in Excel doesn't recognise all methods (run-time error '438')

Time:02-21

I have a custom Class in Excel, and some of its methods are recognised while others aren't.

VBA returns the run-time error '438' object doesn't support this property or method once my module gets to .addButton "button1", "Click Me", "msgbox (""Button clicked!"")" but it has no issue with the previous line, which is a method of the same object.

What could be causing VBA to not recognise the addButton() method, when it recognises createForm() and successfully creates a UserForm?

Module (Example.bas)

Sub Example()

    Dim Form As New UserInterface                                           ' Match Type to UI class module name

    With Form
        .identifier = "example_form"                                        ' A unique identifier for the UserForm
        .title = "Example Window"                                           ' Window title
        .createForm                                                         ' Creates the UserForm
        .addButton "button1", "Click Me", "msgbox (""Button clicked!"")"    ' Adds a button UI element with title and method
        .renderForm                                                         ' Presents the UserForm, then deletes it when closed
    End With

End Sub

ClassModule (UserInterface.bas)

Private formIdentifier As String
Private formTitle As String

Public Property Let identifier(str As String)
    formIdentifier = str
End Property

Public Property Get identifier() As String
    identifier = formIdentifier
End Property

Public Property Let title(str As String)
    formTitle = str
End Property

Public Property Get title() As String
    title = formTitle
End Property

Public Function createForm()

    Set Form = ThisWorkbook.VBProject.VBComponents.Add(3)

    With Form
        .Properties("Name") = formIdentifier
        .Properties("Caption") = formTitle
        .Properties("Width") = 500
        .Properties("Height") = 25
    End With
 
End Function

Public Function addButton(name As String, caption As String, code As String)

    Set Form = ThisWorkbook.VBProject.VBComponents(formIdentifier)
    Set Button = Form.designer.Controls.Add("Forms.commandbutton.1")
    
    With Button
        .Name = name
        .Caption = caption
        .Accelerator = "M"
        .Top = Form.Height
        .Left = 25
        .Width = 450
        .Height = 25
        .Font.Size = 14
        .Font.Name = "Tahoma"
        .BackStyle = fmBackStyleOpaque
    End With
    
    Form.Height = Form.Height   Button.Height   25

    Form.codemodule.insertlines 7, "Private Sub " & name & "_Click()"
    Form.codemodule.insertlines 8, code
    Form.codemodule.insertlines 9, "End Sub"
 
End Function

Public Function renderForm()

    VBA.UserForms.Add(formIdentifier).Show
    ThisWorkbook.VBProject.VBComponents.Remove ThisWorkbook.VBProject.VBComponents(formIdentifier)
 
End Function

CodePudding user response:

The runtime error is not issued by the fact that the method addButton cannot be called. The method itself is raising a runtime error, but depending on your debugger settings, it will or will not stop at the line that causes the error as it is a method within a class (see comment of Storax).

The object form in your class is not of type Userform, it's of type VBComponent. This class has no property like Height that you can access to read and set the height of the form. It has, however, a Property named Properties which is a list of Properties that you can read and modify:

.Top = Form.Properties("Height")
(...)
form.Properties("Height") = form.Properties("Height")   Button.Height   25
 
  • Related