Using VBA to write VBA code (create multiple buttons)
Posted by Luca on October 11, 2001 3:04 AM
By running the following procedure I should create 2 or more buttons inside one specific sheet but I got Excel error and I don't understand the reason.
It works fine if I generate only one button!!
Who can help me?
Luca
Sub AddComm_button()
Dim code As String
Dim j As Integer
Sheets("Sheet1").Select
For j = 1 To 2
'add button
ActiveSheet.OLEObjects.Add ClassType:="Forms.CommandButton.1", _
Left:=126 * j, Top:=96, Width:=126.75, Height:=25.5
'Code for button
code = ""
code = "Sub CommandButton" & Trim(Str(j)) & "_Click()" & vbCrLf
code = code & " Sheets(""Sheet" & Trim(Str(j)) & """).Activate" & vbCrLf
code = code & "End Sub"
'Write code for button
With ThisWorkbook.VBProject.VBComponents("Sheet1").CodeModule
.InsertLines .CountOfLines + 1, code
End With
Next j
End Sub