Trouble assigning macro(args) to dynamically created buttons

2me

New Member
Joined
Jun 1, 2005
Messages
1
This surely isn't the first time someone is asking about this, but after searching for a solution for 4 hours, I think it's time to ask...

Simplistically I'm supposed to do a group of commandbuttons (50-500, amount varies), all of withch should be assigned to a systematically different kind of macro (every button shows a different chart). I've tried to pass a argument through the OnAction- command, but it just doesn't work. I've tried a code like this:


Sub FormsButton_Add()
For i = 1 To 10 '//I'm trying first with 10 buttons
ActiveSheet.Buttons.Add(150, 50 + 50 * i, 50, 50).Select
Sheet1.Shapes(i).OnAction = " ' macro1(" & i & ") ' "
Next i
End Sub

Public Sub macro1(i As Integer)
'code for showing chart number (i)
End Sub

Problem is, that Excel doesn't find the macro1, because I'm messing with the arguments. Without arguments everything goes.

I'm having xl2000 with VBA 6.0
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Why do you have single quotes (') ?
Code:
Sheet1.Shapes(i).OnAction = "macro1(" & i & ")"

By the way why do you have all these buttons, surely there's a better solution, perhaps using a combobox or listbox.

Or getting input from the user.
 
Upvote 0
Try this:

Code:
Sub FormsButton_Add()
Dim i As Long

For i = 1 To 10 '//I'm trying first with 10 buttons
   With ActiveSheet.Buttons.Add(150, 50 + 50 * i, 50, 50)
      .OnAction = "'macro1 " & i & "'"
   End With
Next i
End Sub

Public Sub macro1(i As Integer)
'code for showing chart number (i)
   MsgBox i
End Sub

If you need to pass strings, then careful with the quotes. Look at this post from **** Kusleika:

http://tinyurl.com/9p33g
 
Upvote 0
Juan

You're right - I never tested and just wondered why the single quotes where there.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,331
Members
452,907
Latest member
Roland Deschain

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