View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Michael.Tarnowski Michael.Tarnowski is offline
external usenet poster
 
Posts: 95
Default How to reference Worksheets in VBA properly

On Feb 6, 12:24 am, Gary''s Student
wrote:
A few tiny trick. Say we have a worksheet that may change both its name and
position, but we want to be able to find it.

Pick a cell on the sheet and assing a Defined Name to the cell, say "qwerty"

Once this is done, you can move the sheet around and change its name, but:

Sub dural()
MsgBox (Range("qwerty").Parent.Name)
End Sub

will always give you its correct name and:

Sheets(Range("qwerty").Parent.Name).Activate

will get you there. As I said, a shabby little trick, but it works.
--
Gary''s Student - gsnu2007K

"Michael.Tarnowski" wrote:
Hi experts in the community,


as VBA newbie I know there are two possibilities to reference a
worksheet in an application:


Example: ... Worksheets(workSheetID).Range("openIcon").Value ...


where workSheetID is either:


a.) a numerical ID, the sheet index
or
b.) a string, the name of the sheet as it is displayed in the sheet
tabs.


Question: how can I reference the sheet in VBA without being
intertwined by either sheet name changes or insertions of new sheets
by the user?


Thanks for your help
Have a nice day
Michael


Gary, Dave -- nice suggestions, I will work with that.
But, how should I proceed if I have define global variables for
different worksheets? -- e.g.
const wsShID_1 = "Config"
const wsShID_2 = "Sheet 1"
and so one... ?
Michael