Hi,
I'm trying to create a new sheet with event handling code using vba. I've read and tried heaps of options but none work for me.
My code is:
Sub Macro3()
Dim code As String
Dim NextLine As Integer
Sheets.Add.Name = "Test"
code = "Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)" & vbCrLf & vbCrLf
code = code & "Dim j As Integer" & vbCrLf
code = code & "j = Target.Range.Row" & vbCrLf
code = code & "MsgBox ""row is "" & j" & vbCrLf
code = code & "Call NowTryThis(j)"
code = code & "end sub"
With ActiveWorkbook.VBProject.VBComponents("Test").CodeModule
NextLine = .CountOfLines + 1
.InsertLines NextLine, code
End With
End Sub
I've also tried:
Dim code As String
Dim NextLine As Integer
Sheets.Add.Name = "Test"
With ActiveWorkbook.VBProject.VBComponents("Test").CodeModule
NextLine = .CountOfLines + 1
.InsertLines NextLine, "Text goes here"
End With
It's falling over at the line : With ActiveWorkbook.VBProject.VBComponents("Test").CodeModule
The error message is run-time error 9
Subscript out of range.
I've looked at heaps of examples that don't seem to differ from mine. What am I doing wrong?
Cheers, Ant.
ps How do I get my code to appear in a neat box like other posts I've read?
I'm trying to create a new sheet with event handling code using vba. I've read and tried heaps of options but none work for me.
My code is:
Sub Macro3()
Dim code As String
Dim NextLine As Integer
Sheets.Add.Name = "Test"
code = "Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)" & vbCrLf & vbCrLf
code = code & "Dim j As Integer" & vbCrLf
code = code & "j = Target.Range.Row" & vbCrLf
code = code & "MsgBox ""row is "" & j" & vbCrLf
code = code & "Call NowTryThis(j)"
code = code & "end sub"
With ActiveWorkbook.VBProject.VBComponents("Test").CodeModule
NextLine = .CountOfLines + 1
.InsertLines NextLine, code
End With
End Sub
I've also tried:
Dim code As String
Dim NextLine As Integer
Sheets.Add.Name = "Test"
With ActiveWorkbook.VBProject.VBComponents("Test").CodeModule
NextLine = .CountOfLines + 1
.InsertLines NextLine, "Text goes here"
End With
It's falling over at the line : With ActiveWorkbook.VBProject.VBComponents("Test").CodeModule
The error message is run-time error 9
Subscript out of range.
I've looked at heaps of examples that don't seem to differ from mine. What am I doing wrong?
Cheers, Ant.
ps How do I get my code to appear in a neat box like other posts I've read?