Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Identifiying a Worksheets VBComponent

I am trying to use a macro to add a Selection Change event to a sheet created
in another workbook. The problem that I am having is identifying the
VBComponent for the sheet in question.

I have two workbooks, TargetBook which initially has just one worksheet, and
MacroBook which contains a macro that adds sheets to Targetbook and adds the
Selection Change event to the first new sheet added to TargetBook. When this
macro is run the first sheet added to TargetBook is identifed as Sheet2 in
the VBE and I can define my VBComponent as:

Set VBComp = Workbooks("TargetBook.xls").VBProject.VBComponents ("Sheet2")

However if the user deletes the newsheets and reruns the macro (which they
may want to do) the sheet which should have the event procedure is no longer
"Sheet2" in the project.

Knowing the sheets Excel name and index, how do I identify what the VBE
component name is for the sheet?

Many thanks
Rowan

PS I have looked through Chip Pearson's webpage on the subject but it didn't
answer this question.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Identifiying a Worksheets VBComponent

Maybe something like:

Dim VBComp As Object 'CodeModule
Dim wks As Worksheet
With Workbooks("targetbook.xls")
Set wks = .Worksheets.Add
Set VBComp = .VBProject.VBComponents(wks.CodeName).CodeModule
End With



Rowan wrote:

I am trying to use a macro to add a Selection Change event to a sheet created
in another workbook. The problem that I am having is identifying the
VBComponent for the sheet in question.

I have two workbooks, TargetBook which initially has just one worksheet, and
MacroBook which contains a macro that adds sheets to Targetbook and adds the
Selection Change event to the first new sheet added to TargetBook. When this
macro is run the first sheet added to TargetBook is identifed as Sheet2 in
the VBE and I can define my VBComponent as:

Set VBComp = Workbooks("TargetBook.xls").VBProject.VBComponents ("Sheet2")

However if the user deletes the newsheets and reruns the macro (which they
may want to do) the sheet which should have the event procedure is no longer
"Sheet2" in the project.

Knowing the sheets Excel name and index, how do I identify what the VBE
component name is for the sheet?

Many thanks
Rowan

PS I have looked through Chip Pearson's webpage on the subject but it didn't
answer this question.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Identifiying a Worksheets VBComponent

Thanks Dave

I missed the CodeName property.

Regards
Rowan

"Dave Peterson" wrote:

Maybe something like:

Dim VBComp As Object 'CodeModule
Dim wks As Worksheet
With Workbooks("targetbook.xls")
Set wks = .Worksheets.Add
Set VBComp = .VBProject.VBComponents(wks.CodeName).CodeModule
End With



Rowan wrote:

I am trying to use a macro to add a Selection Change event to a sheet created
in another workbook. The problem that I am having is identifying the
VBComponent for the sheet in question.

I have two workbooks, TargetBook which initially has just one worksheet, and
MacroBook which contains a macro that adds sheets to Targetbook and adds the
Selection Change event to the first new sheet added to TargetBook. When this
macro is run the first sheet added to TargetBook is identifed as Sheet2 in
the VBE and I can define my VBComponent as:

Set VBComp = Workbooks("TargetBook.xls").VBProject.VBComponents ("Sheet2")

However if the user deletes the newsheets and reruns the macro (which they
may want to do) the sheet which should have the event procedure is no longer
"Sheet2" in the project.

Knowing the sheets Excel name and index, how do I identify what the VBE
component name is for the sheet?

Many thanks
Rowan

PS I have looked through Chip Pearson's webpage on the subject but it didn't
answer this question.


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBIDE.VBComponent michaelberrier Excel Discussion (Misc queries) 3 June 12th 06 05:47 PM
VBComponent Types Todd Huttenstine Excel Programming 1 May 11th 04 08:16 PM
Compile Error on "dim x as VBComponent"? PatFinegan[_7_] Excel Programming 2 January 29th 04 10:00 PM
Relating Worksheet(I).Name with VBComponent(J).Name MWE[_5_] Excel Programming 1 January 20th 04 08:51 PM
problem with wkbk.VBProject.VBComponent Rich[_14_] Excel Programming 0 September 11th 03 07:34 PM


All times are GMT +1. The time now is 05:29 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"