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

I have multiple worksheets, in my workbook, each renamed from original
"Sheet#" name. Is there a way to still reference each sheet in a macro as
Sheet1, Sheet2...., or can you only reference them by their current
caption-name?

Thanks,
Les
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Referencing Worksheets

The best way is to reference a worksheet by its name because you can move the
order of the worksheets and then the macro will not work.

use either
worksheets("Sheet2")
or
worksheets(2)
or
sheets("Sheet2")
or
sheets(2)



"WLMPilot" wrote:

I have multiple worksheets, in my workbook, each renamed from original
"Sheet#" name. Is there a way to still reference each sheet in a macro as
Sheet1, Sheet2...., or can you only reference them by their current
caption-name?

Thanks,
Les

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Referencing Worksheets

If you want your code to be bug proof you should not refer to sheets by their
tab name as tab names often get changed over time. Worksheets are objects and
can be referenced primarily using one of three ways. By index, by tab name or
by code name.

Index and tab name are generally not a good idea as they can change as
sheets are added or deleted or as tabs are renamed. When that happens code
either crashes or worse still operates on the wrong sheet.

Code name is really the best way to go. In the VBE the project explorer will
show you each sheet with something like Sheet1(MyTabName). Sheet1 is the code
name and MyTabName is the caption. You can directly reference the sheet in
code using Sheet1. So these two lines of code are roughly equivalent.

msgbox sheet1.name
or
msgbox worksheets("MyTabName").name

The difference is that Sheet1 refers directly to the worksheet object and is
defined at compile time. Thus you get an intellisence dropdown when you type
Sheet1. The other is only defined at run time so you do not get an
intellisence drop down.

Note that you can change the code name of the sheet by changing the (Name)
preperty in the properties window.

--
HTH...

Jim Thomlinson


"WLMPilot" wrote:

I have multiple worksheets, in my workbook, each renamed from original
"Sheet#" name. Is there a way to still reference each sheet in a macro as
Sheet1, Sheet2...., or can you only reference them by their current
caption-name?

Thanks,
Les

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 141
Default Referencing Worksheets

On Apr 8, 1:51*pm, Jim Thomlinson <James_Thomlin...@owfg-Re-Move-
This-.com wrote:
If you want your code to be bug proof you should not refer to sheets by their
tab name as tab names often get changed over time. Worksheets are objects and
can be referenced primarily using one of three ways. By index, by tab name or
by code name.

Index and tab name are generally not a good idea as they can change as
sheets are added or deleted or as tabs are renamed. When that happens code
either crashes or worse still operates on the wrong sheet.

Code name is really the best way to go. In the VBE the project explorer will
show you each sheet with something like Sheet1(MyTabName). Sheet1 is the code
name and MyTabName is the caption. You can directly reference the sheet in
code using Sheet1. So these two lines of code are roughly equivalent.

msgbox sheet1.name
or
msgbox worksheets("MyTabName").name

The difference is that Sheet1 refers directly to the worksheet object and is
defined at compile time. Thus you get an intellisence dropdown when you type
Sheet1. The other is only defined at run time so you do not get an
intellisence drop down.

Note that you can change the code name of the sheet by changing the (Name)
preperty in the properties window.

--
HTH...

Jim Thomlinson



"WLMPilot" wrote:
I have multiple worksheets, in my workbook, each renamed from original
"Sheet#" name. *Is there a way to still reference each sheet in a macro as
Sheet1, Sheet2...., or can you only reference them by their current
caption-name?


Thanks,
Les- Hide quoted text -


- Show quoted text -


To clarify, you change the name of the sheet in the properties
window ... once you're in VBE.

Chris
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
referencing worksheets kmr Excel Discussion (Misc queries) 0 June 3rd 08 03:32 PM
Referencing worksheets jbaldwin Excel Discussion (Misc queries) 2 July 4th 07 10:11 PM
Referencing worksheets yukon_phil Excel Discussion (Misc queries) 5 September 1st 06 08:25 PM
Referencing Worksheets Cincy Excel Discussion (Misc queries) 4 July 19th 06 08:59 PM
Referencing worksheets NewExcelGuy Excel Worksheet Functions 2 September 11th 05 05:26 PM


All times are GMT +1. The time now is 01:10 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"