Programatically Add Button

NicholasP

Active Member
Joined
Nov 18, 2006
Messages
291
Hi,

I'm trying to use VB to add a button in Excel. I've been playing around with this for the better part of the day, and I can't seem to figure it out.

So far, this is what I have:

Code:
x = ActiveWorkbook.Name
ActiveSheet.Buttons.Add(700, 15, 100, 37.5).Select

Selection.OnAction = x & "!ShowSheets"
Selection.Characters.Text = "Show Calculation Sheets"


ActiveSheet.Buttons.Add(700, 68, 100, 37.5).Select

With Selection
    .OnAction = x & "!HideSheets"
    .Characters.Text = "Hide Calculation Sheets"
End With

The problem is, this doesn't work (although it was working when I had it in the test file, so I'm not sure what I changed to make this not work).

If anyone has any suggestions, I'd be very grateful! Also, any links that anyone might think are useful for dealing with this topic would be much appreciated as well.

Thanks
Nick
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
What specificaly doesn't work?
Also, have you used the macro recorder to get the code you want.
 
Upvote 0
I used the macro recorder, but it only adds a button and I'm not sure how to apply what I want to do after that....this code succesfully adds the buttons, but it errors on the lines of code:

Code:
Selection.OnAction = x & "!ShowSheets"

Code:
 .OnAction = x & "!HideSheets"

Basically two ways to say the same thing...the error states:

"Unable to set the OnAction property of the Button class"

Any thoughts?
 
Upvote 0
This worked for me
Code:
With ActiveSheet.Buttons.Add(700, 15, 100, 37.5)
    .OnAction = "ShowSheets"
    .Characters.Text = "Show Calculation Sheets"
End With
 
Upvote 0
yeah, I took out the reference to the Activeworkbook and it worked for me too....thanks!

Nick
 
Upvote 0

Forum statistics

Threads
1,215,771
Messages
6,126,798
Members
449,337
Latest member
BBV123

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