Create new worksheet with Event Handler code with vba.

antmud

New Member
Joined
Jul 7, 2012
Messages
15
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?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi

A sheet has 2 names

- the Name that the user can see in the tabs and can change at will

- the CodeName, that is a name automatically created by excel when you create the sheet and that the user does not see nor can change (unless it's a power user :) )

VbComponents() expects the codename, not the name. You create the worksheet and change its name to "Test" but the codename remains the same, for ex. "Sheet8" and that's the one you must use with VbComponents()

Try this example that add a worksheet and writes something to its module:


Code:
Sub test()
Dim sCodeName As String

Worksheets.Add.Name = "Test"

' Get the codename of the newly created sheet
sCodeName = Worksheets("Test").CodeName

' Write some text in the module
With ActiveWorkbook.VBProject.VBComponents(sCodeName).CodeModule
    If .CountOfLines > 0 Then .DeleteLines (1)
    .AddFromString ("Rem This is a test")
End With
End Sub

For posting code click on the Go Advanced button and then on the # button in the Editor
 
Upvote 0
Thank you, that has given me renewed hope.

I've tried your code and also updated my code. I'm now getting a message:

"Can't enter break mode at this time".

In your code it comes at .deletelines (1)

In my code it comes at .InsertLines NextLine, code

Apart from that the new worksheet is being created and code is appearing in the right place.

Hoping you can help me on this too.

Thanks, Ant.
 
Upvote 0
Hi Ant

This is probably because you tried to execute this code in single step, which you cannot do with this type of code.

Try:

Create a new workbook and execute my code (Run, not Single Step).




Remark: I think it's interesting this possibility of writing code using vba but I don't think I'd use it for a real solution. Unless this is really a very special case I don't see why you'd do it.
 
Upvote 0
Why not use the SheetFollowHyperlink workbook level event?
Code:
Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)

End Sub
This is triggered clicked whenever a hyperlink on a worksheet is clicked.

The hyperlink and the sheet it's on are passed to the sub.
 
Upvote 0
Thanks for your help PGC, I'm well on my way to the next step now.

My macro creates a number of sheets for which I wanted to create hyperlinks to further macros that would be run as required. I've since found out that there is a hyperlink as a workbook event, so I'm now putting the code into the workbook code (as Norie has also suggested).

Whenever I'm tackling a macro I tend to think of the saying " One small step for man ... "

Thanks again for your help.
 
Upvote 0

Forum statistics

Threads
1,222,749
Messages
6,167,971
Members
452,158
Latest member
MattyM

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top