I am trying to create a formula to first create a new page, and then add a button with code to that page, using only Visual Basic code. When I run the macro, I get the error 9, subscript out of range. Any explanations?
EDIT: added code tags - Smitty
Code:
Sub CreateGraphPg()
Numsheets = ActiveWorkbook.Sheets.Count
Dim NewSheet As Worksheet
Dim NewButton As OLEObject
Dim wBook As Workbook
Dim Code As String
Set wBook = ActiveWorkbook
Set NewSheet = Sheets.Add
NewSheet.Move After:=Sheets(Numsheets + 1)
NewSheet.Name = "Graphs"
Set NewButton = NewSheet.OLEObjects.Add("Forms.CommandButton.1")
With NewButton
.Left = 280
.Top = 15
.Width = 100
.Height = 25
.Object.Caption = "Graph"
End With
Code = "Private Sub CommandButton1_Click()" & vbCrLf
Code = Code & " MakeGraph" & vbCrLf
Code = Code & "End Sub"
With wBook.VBProject.VBComponents(NewSheet.Name).CodeModule
Nextline = .CountOfLines + 1
.InsertLines Nextline, Code
End With
End Sub
EDIT: added code tags - Smitty