excel vba dynamically generated

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

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

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

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.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Counting Blanks

Some times you want to check if there are cells missing data in your range. You can use the COUNTBLANK FUNCTION to acheive this. It is =COUNTBLANK(Range). Note Cells with formulas that return "" (empty text) are also counted. Cells with zero values are not counted.

View all Excel hints and tips


Server loaded in 0.09 secs.