1

Per Mathieu's reply, I managed to create an ActiveX button via

Sub aaaaaaaa()
Dim newButton As Object
Set newButton = Sheets(sheetname_KvE).OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
    Link:=False, _
    DisplayAsIcon:=False, _
    Left:=800, _
    Top:=0, _
    Width:=300, _
    Height:=30).Object
With newButton
    .Caption = "bla"
    '.Name = "Button_what" ' - THIS DOES NOT WORK!
End With
End Sub

How can I rename that button?

Unfortunately, these links did not lead me to the solution:

This would be quite glorious, as it enables one to add relevant code in the sheet's module (as far as I can see, they relevant sub is always called [insert button name]_Click).

mvidude
  • 41
  • 7
  • Does this answer your question? [Change Name of ActiveX Command Button using VBA in Excel](https://stackoverflow.com/questions/10641683/change-name-of-activex-command-button-using-vba-in-excel) – Andreas Nov 26 '20 at 09:48
  • @Andreas I could have tried Siddarth's bit under his "MORE FOLLOWUP" there but I did not get around to it. Fortunately, he even replied here. :) But thank you for the suggestion! – mvidude Nov 26 '20 at 11:15

1 Answers1

1

Is this what you are trying?

Sub Sample()
    Dim newButton As OLEObject
    Dim ws As Worksheet
    
    Set ws = Sheets(sheetname_KvE)
    Set newButton = ws.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
                                      Link:=False, _
                                      DisplayAsIcon:=False, _
                                      Left:=800, _
                                      Top:=0, _
                                      Width:=300, _
                                      Height:=30)
    newButton.Object.Caption = "bla"
    newButton.Name = "Button_what"
End Sub
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • I am super confused, but yes, thank you so much. :) Note: I prefer With-ing stuff, so I tried to do that with your last 2 lines and it worked! – mvidude Nov 26 '20 at 11:14
  • `With-ing` is good if you have many lines... For 2 lines it doesnt make any sense but then it is a personal choice :) – Siddharth Rout Nov 26 '20 at 11:57
  • It is _incredibly_ noteworthy that there seems to be a character limit for the button names. From what I can tell, it seems to be 24. :| Found that out the hard way. :( – mvidude Nov 30 '20 at 10:16
  • 1
    For Form Controls it is 31 and not 24. You can manully try and rename your button to `1234567890123456789012345678901` (31 chars) but not `12345678901234567890123456789012` (32 chars) – Siddharth Rout Nov 30 '20 at 10:45