ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Using code to create a control on an Excel userform (https://www.excelbanter.com/excel-programming/317156-using-code-create-control-excel-userform.html)

Fred Holmes

Using code to create a control on an Excel userform
 
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


Andy Pope

Using code to create a control on an Excel userform
 
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

Fred Holmes wrote:

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


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info


All times are GMT +1. The time now is 11:14 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com