excel vba dynamically generated
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Excel VBA: Dynamically generated userform. How to add control ma

Excel VBA: Dynamically generated userform. How to add control ma

resolvedResolved · Medium Priority · Version 2007

replyReply Wed 12 Jan 2011, 13:07Delegate Harald said...

Harald has attended:
Excel VBA Intro Intermediate course

Excel VBA: Dynamically generated userform. How to add control ma

I generate a dynamic userform with a procedure and add an commandbutton by usind the following code:

Set mycmd = MyForm.designer.Controls.Add("Forms.CommandButton.1")
With mycmd
.Caption = "OK"
.Top = t + 5 ' move button to the bottom
.Left = 30
.Height = 20
.Width = 50 'Lenth of label field for tick box
.Font.Size = 10
.Font.Bold = True 'makes text bold
.Font.Name = "Arial"
.Visible = True
End With

How can I add a private sub in order to get the OK button to execute the below macro like:

Private Sub CommandButton1_Click()

MsgBox "OK button pressed"
End Sub

As I generate the button in my procedure I can not click on it and do a private sub. If I write the private sub in the same module underneath my main code it does not do anything.

Regards,
Harald

For upcoming training course dates see: Pricing & availability

replyReply Wed 12 Jan 2011, 15:48Trainer Anthony said...

RE: Excel VBA: Dynamically generated userform. How to add contro

Hi Harald, thanks for your query. In your procedure, when you've generated the commandbutton on the fly, your next line of code - with the commandbutton still selected, of course, should be:

Selection.OnAction = "MyMacro"

Hope this helps,

Anthony

Wed 19 Jan 2011: Automatically marked as resolved.

 

Please browse our web site to find out more about
excel classes and other Microsoft training courses.

Excel tip:

Copying Formulae Quickly

There is a quicker way of copying a formula down a column.

Just point and double click on the black autofill handle in the right hand corner of a cell.

This will work providing you have some data in the column to the left of the column.

Sandy

View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard