View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming,microsoft.public.scripting.vbscript
Robert Stober Robert Stober is offline
external usenet poster
 
Posts: 26
Default Creating Event procedures from a macro

Hi,

I'm using Jon Peltier's workaround (to Excel's inability to create
hyperlinks to chart sheets) to create a table of contents. The following
event procedure is placed in the code module for the "contents" sheet. It
activates the chart sheet when the user selects the linked cell:

' Thank you Jon!
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B2")) Is Nothing Then
Charts("Chart1").Activate
End If
End Sub

This works fine, except that I need to create it on the fly from within a
macro. Here's what I've got so far:

ActiveWorkbook.VBProject.VBComponents("Contents"). CodeModule.InsertLines _

ActiveWorkbook.VBProject.VBComponents("Contents"). CodeModule.CreateEventProc
( _
"SelectionChange", "Worksheet") + 1, _
"MsgBox Hello"
Where "Contents" is the name of the worksheet where I want the event
procedure. The actual code I want to invoke inside the event procedure isn't
really "MsgBox Hello", but I can't even get this simple code to work. I get
"subscript out of range"....

For those of you who want to know more, the code I really want instead of
MsgBox is:

"If Not Intersect(Target, Range("B2")) Is Nothing Then
Charts("Chart1").Activate
End If"

I know this is a hard one. Can anyone provide any suggestions?

Thank you,

Robert Stober