views:

4009

answers:

5

I'm creating buttons dynamically on an Excel userform with the following code:

With Me.CurrentFrame.Controls.Add("Forms.CommandButton.1")
    .Caption = "XYZ"
    .name = "AButton"
    .Font.Bold = True
    .ForeColor = &HFF&
    ... blah blah blah
End With

I'd like to assign a function to run when these buttons are clicked, but I can't find a straightforward way to do this since there's no property as part of the button itself.

Is there a way to do this using the above idiom? Should I be going about this whole thing in a different way?

+4  A: 

You need to dynamically create code / event handlers for each button.

It take a bit of doing - see here: http://navpadexcel.blogspot.com/2006/11/httpwwwcpearsoncomexcelvbehtm.html

A better way might be to create a bunch of buttons on the form (as many as you think you'll need) ahead of time. Create the event handler code as well. Make them all hidden initially.

Then when your form opens you can dynamically change the button captions, make them visible and move them around. The event code you created initially will be linked to the activated buttons as expected.

DJ
Clever idea! Seems everything in VBA requires being clever.
notnot
A: 

Every search on the net boils down to this code:

 Dim NewButton As Control  
 Dim CodeModule As Object  

Set NewButton = Me.Controls.Add("Forms.CommandButton.1", "MyButton", True)  
NewButton.Caption = "Click me!"  
Set CodeModule = ActiveWorkbook.VBProject.VBComponents.VBE.ActiveCodePane.CodeModule  
CodeModule.InsertLines CodeModule.CreateEventProc("Click", NewButton.Name) + 1, vbTab & "MsgBox ""Hello world"""

But I have tried all examples and cant get them to work.

Stefan
A: 

I've been looking at this as well. Seems you can run a macro by using the onClick property:

Command1.OnClick = "Macro1"

Then create a macro by that name that runs the desired function. This is my hack around this until I find something better.

A: 

The below should be working

Dim NewButton As OLEObject
Dim CodeModule As Object

Set NewButton = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
    Link:=False, DisplayAsIcon:=False, Left:=52.5, Top:=Hght, _
    Width:=202.5, Height:=26.25)
NewButton.Object.Caption = "Click Me!"
Set CodeModule = ActiveWorkbook.VBProject.VBComponents.VBE.ActiveCodePane.CodeModule
CodeModule.InsertLines CodeModule.CreateEventProc("Click", NewButton.Name) + 1, vbTab & "MsgBox ""Hello world"""
Alex
A: 

Sub Oval1_Click() file = ActiveWorkbook.Name Set Output = Workbooks.Add() ActiveWorkbook.SaveAs Filename:="Try.xls"

Sheets(1).Select

ActiveSheet.Buttons.Add(460, 10, 140, 30).Select ActiveSheet.Buttons.Text = "DATA"

ActiveSheet.Shapes("Button 1").Select Selection.OnAction = "Book1.xlsm!data_Click"

End Sub

Sub data_Click()

MsgBox "you have clicked me"

ActiveSheet.DrawingObjects.Delete

End Sub

Priyanka
I mean for buttons added to userforms, not to the worksheet
notnot