Forum

Using Macro to add ...
 
Notifications
Clear all

Using Macro to add and name command button

4 Posts
2 Users
0 Reactions
389 Views
(@mdevola89)
Posts: 10
Active Member
Topic starter
 

Dim cb As Shape
Set cb = ActiveSheet.Shapes.AddFormControl(xlButtonControl, 625, 55, 60, 50)
cb.OnAction = "UnprotectWorksheets"
ActiveSheet.Buttons("Button 13").Caption = "Change DMR"

 

I am using this code to add and name a command button to my worksheet, which works the first time used. Unfortunately, the command button number changes each time it is run, so in this line (ActiveSheet.Buttons("Button 13").Caption = "Change DMR") the Button name is different ant the caption "Change DMR" does not get applied. I have tried a few different options, but can't get it to work. Any suggestions??

 
Posted : 14/08/2021 10:43 am
 A S
(@ac-porta-via)
Posts: 6
Active Member
 

Try this

Sub ADD_BUTTON_UnprotectWorksheets()
Dim ws As Excel.Worksheet
Dim btn As BUTTON

With ActiveWorkbook.ActiveSheet
Set btn = ActiveSheet.Buttons.Add(625, 55, 60, 50)
btn.OnAction = "UnprotectWorksheets"
btn.Caption = "Change DMR"
btn.Font.Size = 11
btn.Font.Name = "CALIBRI"
End With
End Sub

 
Posted : 16/08/2021 1:40 pm
(@mdevola89)
Posts: 10
Active Member
Topic starter
 

Thank you, this works great.

 
Posted : 17/08/2021 7:53 am
 A S
(@ac-porta-via)
Posts: 6
Active Member
 

You are Welcome 🙂

 
Posted : 17/08/2021 3:14 pm
Share: