R
Rocky
Trying to learn how to use vba to write vba code. Copied this code from
Walkenbach's book to watch how it runs, but when i try to run it, I get the
error message "cant' enter break mode" on
Set NewButton = NewSheet.OLEObjects.Add("Forms.CommandButton.1")
Sub AddSheetAndButton()
Dim NewSheet As Worksheet
Dim Newbutton As OLEObject
Dim Code As String
Dim NextLine As Integer
'Add the sheet
Set NewSheet = Sheets.Add
'Add a CommandButton
Set Newbutton = NewSheet.OLEObjects.Add("Forms.CommandButton.1")
With Newbutton
.Left = 4
.Top = 4
.Width = 100
.Height = 24
.Object.Caption = "Return to Sheet1"
End With
'Add the event handler code
Code = "Sub CommandButton1_Click()" & vbCrLf
Code = Code & " On Error Resume Next" & vbCrLf
Code = Code & " Sheets(""Sheet1"").Activate" & vbCrLf
Code = Code & " If Err <>0 Then" & vbCrLf
Code = Code & " MsgBox ""Cannot activete Sheet1.""" & vbCrLf
Code = Code & " End If" & vbCrLf
Code = Code & "End Sub"
With ActiveWorkbook.VBProject.VBComponents(NewSheet.Name).CodeModule
NextLine = .CountOfLines + 1
.InsertLines NextLine, Code
End With
End Sub
Have not set any breakpoints in the module.
Any ideas on what could be triggering this?
Tx
Walkenbach's book to watch how it runs, but when i try to run it, I get the
error message "cant' enter break mode" on
Set NewButton = NewSheet.OLEObjects.Add("Forms.CommandButton.1")
Sub AddSheetAndButton()
Dim NewSheet As Worksheet
Dim Newbutton As OLEObject
Dim Code As String
Dim NextLine As Integer
'Add the sheet
Set NewSheet = Sheets.Add
'Add a CommandButton
Set Newbutton = NewSheet.OLEObjects.Add("Forms.CommandButton.1")
With Newbutton
.Left = 4
.Top = 4
.Width = 100
.Height = 24
.Object.Caption = "Return to Sheet1"
End With
'Add the event handler code
Code = "Sub CommandButton1_Click()" & vbCrLf
Code = Code & " On Error Resume Next" & vbCrLf
Code = Code & " Sheets(""Sheet1"").Activate" & vbCrLf
Code = Code & " If Err <>0 Then" & vbCrLf
Code = Code & " MsgBox ""Cannot activete Sheet1.""" & vbCrLf
Code = Code & " End If" & vbCrLf
Code = Code & "End Sub"
With ActiveWorkbook.VBProject.VBComponents(NewSheet.Name).CodeModule
NextLine = .CountOfLines + 1
.InsertLines NextLine, Code
End With
End Sub
Have not set any breakpoints in the module.
Any ideas on what could be triggering this?
Tx