Thread
:
Creating Event procedures from a macro
View Single Post
#
2
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming,microsoft.public.scripting.vbscript
Chip Pearson
external usenet poster
Posts: 7,247
Creating Event procedures from a macro
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
Reply With Quote
Chip Pearson
View Public Profile
Find all posts by Chip Pearson