Public Function CreateForm1()
'MAKE SURE YOU ADD THE FOLLOWING REFERENCES ON THE MAC
' Microsoft Visual Basic for Applications Extensibility 5.3
' vbapp type library
Dim form1Name As String: form1Name = "testForm1"
Dim form1 As VBComponent
Dim lbl1 As MSForms.Label
Dim formExists As Boolean
Dim i As Long
For i = 1 To ThisWorkbook.VBProject.VBComponents.Count
With ThisWorkbook.VBProject.VBComponents(i)
If .Type = vbext_ct_MSForm Then
If .Name = form1Name Then
formExists = True
Exit For
End If
End If
End With
Next
If Not formExists Then
Set form1 = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
With form1
.Properties("Height") = 150
.Properties("Width") = 200
On Error Resume Next
.Name = form1Name
.Properties("Caption") = "Dynamic Label Form"
Dim btn As MSForms.CommandButton
Set btn = .Designer.Controls.Add("forms.CommandButton.1")
With btn
.Caption = "Cancel"
.Height = 18
.Width = 44
.Left = CLng(form1.Properties("Width") / 2) - CLng(btn.Width)
.Top = 5
End With
Set btn = .Designer.Controls.Add("forms.CommandButton.1")
With btn
.Caption = "OK"
.Height = 18
.Width = 44
.Left = CLng(form1.Properties("Width") / 2) + 1
.Top = 5
End With
Set lbl1 = .Designer.Controls.Add("Forms.Label.1")
With lbl1
.Caption = "I'm a Label"
.AutoSize = True
End With
On Error Resume Next
With .CodeModule
Dim X As Long
X = .CountOfLines
.InsertLines X + 1, "Sub CommandButton1_Click()"
.InsertLines X + 2, " Unload Me"
.InsertLines X + 3, "End Sub"
.InsertLines X + 4, ""
.InsertLines X + 5, "Sub CommandButton2_Click()"
.InsertLines X + 6, " Unload Me"
.InsertLines X + 7, "End Sub"
End With
End With
End If
End Function