Jen Payne
asked on
VBA - Create Command buttons
Hi
I'm trying to find a way to create command buttons via VBA. I would also like to rename them and attach a code to them too.
I would like the code to sit within the worksheet.
I'm trying to find a way to create command buttons via VBA. I would also like to rename them and attach a code to them too.
I would like the code to sit within the worksheet.
ASKER
Hi,
Thanks, is there a way to add a command button (active x control) instead, because my code is in the sheet module?
Thanks, is there a way to add a command button (active x control) instead, because my code is in the sheet module?
then try
Sub AddingButtons()
Dim t As Range
Dim Obj As Object
Dim Code As String
ShtNm = ActiveSheet.Name
Set t = ActiveSheet.Range("B5:F6")
Set Obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=t.Left, Top:=t.Top, Width:=t.Width, Height:=t.Height)
Obj.Caption = "Show Data Selection Window"
Code = "Private Sub CommandButton1_Click()" & vbCrLf
Code = Code & "Call MySub(ShtNm)" & vbCrLf
Code = Code & "End Sub"
With ActiveWorkbook.VBProject.VBComponents(Worksheets(ShtNm).CodeName).CodeModule
.insertlines .CountOfLines + 1, Code
End With
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
found an example and answer on another site
Pls try
Open in new window
Regards