View Single Post
  #4   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

The line breaks shown in my last posting don't reflect the actual line
breaks in the macro. Basically, here's the posting again with more attention
paid to the line breaks as they might be displayed...


With ActiveWorkbook.VBProject.VBComponents( _
ActiveWorkbook.Worksheets("Contents").CodeName).Co deModule
.InsertLines Line:=.CreateEventProc("SelectionChange", "Worksheet") + 1,
_
String:="If Not Intersect(Target, Range(""" & _
nextCell & """)) Is Nothing Then" & vbCrLf & _
" Charts(""" & chartName & """).Activate" & vbCrLf & _
"End If"
End With

Hopefully this makes the placement of the line breaks more clear.

Thanks again,

Robert

"Chip Pearson" wrote in message
...
Robert,

Try the following code. Watch out for line breaks.

With ActiveWorkbook.VBProject.VBComponents( _
ActiveWorkbook.Worksheets("Contents").CodeName).Co deModule
.InsertLines Line:=.CreateEventProc("SelectionChange", "Worksheet") +

1,
_
String:= _
"If Not Intersect(Target, Range(""B2"")) Is Nothing Then" & vbCrLf

&
_
" Charts(""Chart1"").Activate" & vbCrLf & _
"End If"
End With



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com



"Robert Stober" wrote in message
...
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