Add macro button at end of script?

Desu Nota from Columbus

Well-known Member
Joined
Mar 17, 2011
Messages
556
I have a two step process that I now have working perfectly thanks to various resources and people on this forum. Awesome.

Now I want to make it esthetically pleasing and simple for my co-workers to use.

I tried various ways of recording/writing code to add a button which when clicked runs a macro (fail).

I would like the button to be added to worksheet "Results" and fit in Column D (column width = 18) at rows 5+6 (row height combined = 25).

I am not sure if you place a button based on columns/rows or by some other means so I will add my recorded macro just in case reference is needed.

Code:
Range("D5").Select
    Application.CommandBars("Formatting").Controls.Add Type:=msoControlButton, _
        ID:=282, Before:=14
    ActiveSheet.Buttons.Add(149.25, 52.5, 89.25, 24.75).Select
    Selection.OnAction = "PERSONAL.XLS!ATest2A"
    ActiveSheet.Shapes("Button 1").Select
    Selection.Characters.Text = "Get Downtime Data"
    With Selection.Characters(Start:=1, Length:=17).Font
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    Selection.ShapeRange.ScaleWidth 1.43, msoFalse, msoScaleFromTopLeft
    Selection.ShapeRange.ScaleHeight 1.45, msoFalse, msoScaleFromTopLeft
    Range("E10").Select
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Why not just add the ActiveX button to your worksheet, place it where ever you want, size it, and change the properties to suit (colour / caption etc) then set the visible property to "False"?
Put your required code in the button's _click event.

Make the final line of your other code:
Code:
Me.CommandButton1.Visible = True
.....which will make the button visible to the user, then they press it - which will run your second code.

make the last line of the button's code:
Code:
Me.CommandButton1.Visible = False
.....to automatically hide the button again.

Hopefully, you get the idea - even if I've not understood your request quite correctly - i.e. don't try and programatically build the button - build it into your spreadsheet, then use code to change it's visibility properties.
 
Upvote 0
I'm not quite sure exactly what you mean.

Everytime I run these macros, they will be in a new workbook. I need the macro to add the button because the sheet where I would have hidden the button doesn't exist until the macro is run.

I think to do what you are suggesting I would need to restructure my entire code to create a workbook that can be reused (which I would like to do, but I am not adept enough to do it).
 
Last edited:
Upvote 0
Hi

There was nothing at all in your post suggesting a new workbook. The request was "........to add a button which when clicked runs a macro "

Hopefully someone else will be able to help on this.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,754
Members
452,940
Latest member
rootytrip

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