I am having issues assigning a code to a command button that is added to a worksheet after it is created. No issues with creating the commnad button itself. Just cant get the code to work. All the new button will do is return the user to the "Main" page. Thanks
Dim Obj As Object
Dim Code As String
'create button
Set Obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=200, Top:=100, Width:=100, Height:=35)
Obj.Name = "MainButton"
'buttonn text
ActiveSheet.OLEObjects(1).Object.Caption = "Return to Main page"
'macro text
Code = "Private Sub MainButton_Click()" & vbCrLf
Code = Code & "Call Main" & vbCrLf
Code = Code & "End Sub"
'add macro at the end of the sheet module
With ActiveWorkbook.VBProject.VBComponents(WS.Name).CodeModule
.insertlines .CountOfLines + 1, Code
End With
End sub
Sub Main()
Worksheets("Main").Select
End Sub
Dim Obj As Object
Dim Code As String
'create button
Set Obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=200, Top:=100, Width:=100, Height:=35)
Obj.Name = "MainButton"
'buttonn text
ActiveSheet.OLEObjects(1).Object.Caption = "Return to Main page"
'macro text
Code = "Private Sub MainButton_Click()" & vbCrLf
Code = Code & "Call Main" & vbCrLf
Code = Code & "End Sub"
'add macro at the end of the sheet module
With ActiveWorkbook.VBProject.VBComponents(WS.Name).CodeModule
.insertlines .CountOfLines + 1, Code
End With
End sub
Sub Main()
Worksheets("Main").Select
End Sub