Assign command to CommandButton using VBA Code

vodkasoda

Board Regular
Joined
Aug 12, 2004
Messages
86
I am creating details on a Userform within VBA code as follows :

Code:
For A = 1 To MyUniqueNo
    MyLeft = 14
    UserForm3c.Frame1.ScrollBars = fmScrollBarsVertical
    UserForm3c.Frame1.ScrollHeight = UserForm3c.Frame1.Height * (MyUniqueNo / 18)
    Set MyButton = UserForm3c.Frame1.Controls.Add("Forms.CommandButton.1")
    With MyButton
        .Caption = A
        .Width = 42
        .Height = 18
        .Left = MyLeft
        .Top = MyTop
    End With
    MyLeft = MyLeft + 42
Next
UserForm3c.Show

The screen works fine, but I now want to add a Command to each CommandButton that is created in this loop, just to display a new screen, but I can't figure out what to use. I tried

Code:
.OnAction = "Call Update_Player"

but I get a "Run-Time error 438, Object doesn't support this property or method" ...

How can I do this ?

Just had a thought, should I be creating xxx_Click routines for each button (or even 1 for any button & passing the parameters) ? If so, what do I call it ... what are my CommandButtons called ... oh dear, that looks like a stupid question ?!?!?!?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
this line may be necessary somewhere in the beginning

Code:
Application.CommandBars("Forms").Visible = True


and then on action code try this

Code:
.OnAction = "Update_Player"
post feedback
 
Upvote 0
It makes no difference, I'm afraid ...

I put some test MsgBoxes in & discovered that my CommandButtons are being named logically, CommandButton1, CommandButton2 etc., but I have scoured the Internet & cannot find a way to assign an Action to these Command Buttons. In fact it seems I am not the only person having this problem ...

I have found a way of doing it if the UserForm is temporary (http://scriptorium.serve-it.nl/view.php?sid=13) but I can't seem to tweak this so that the Command Buttons work when they are added to a UserFrom that has been created using the design tool.

Surely there is somebody on here that knows how to do something that, on the face of it, looks so simple ?!?!?
 
Upvote 0
my CommandButtons are being named logically, CommandButton1,


see this code

Code:
Application.CommandBars("Forms").Visible = True
    ActiveWindow.SmallScroll Down:=4
    ActiveSheet.Buttons.Add(588, 164.25, 138, 24.75).Select
Selection.Name = "create_graph"


the cruicial line is
Code:
Selection.Name = "create_graph"

this will give a name to the button instead of default namae like commandbutton1 etc by excel.
 
Upvote 0
Again, no luck ... I can call the Command Buttons anything I like, but if I pre- create _Click event subroutines, both in the UserForm code and in the executing Module code, it takes no effect ... very frustrating indeed !!!


see this code

Code:
Application.CommandBars("Forms").Visible = True
    ActiveWindow.SmallScroll Down:=4
    ActiveSheet.Buttons.Add(588, 164.25, 138, 24.75).Select
Selection.Name = "create_graph"


the cruicial line is
Code:
Selection.Name = "create_graph"

this will give a name to the button instead of default namae like commandbutton1 etc by excel.
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,700
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top