'VBA ClassModule in Excel doesn't recognise all methods (run-time error '438')
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
