Using code to create a control on an Excel userform

F

Fred Holmes

In Visual Basic 6 (not VBA) I can create a control on a UserForm with
working code such as:

Private Sub Form_Load()
Set lblMyLabel = Me.Controls.Add("vb.label", "lblMyLabel", Me)

I'm trying to do the same thing with a userform in VBA in Excel 2003.

Private Sub UserForm_Initialize()
Set lblMyLabel = Me.Controls.Add("vb.label", "lblMyLabel", Me)

With this code included, I get an error message "Invalid Class String"
on the UserForm1.Show statement.

Is it possible in Excel vba to create a control with a Set statement?

What's the correct syntax? Have I omitted some Dim Statement???
I've: Dim lblMyLabel As Label

Thanks,

Fred Holmes
 
A

Andy Pope

Hi Fred,

Try this code.

'---
Dim labTemp As MSForms.Label

Set labTemp = Me.Controls.Add("Forms.Label.1", "MyLabel", True)
With labTemp
.Caption = "Hello World"
.BorderStyle = fmBorderStyleNone
End With
'---

Cheers
Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top