How to reference Worksheets in VBA properly
The codename is much more difficult for the user to screw up.
Open the VBE
Hit ctrl-r to see the project explorer
Expand one of the projects
Under the Microsoft Excel Objects, you'll see the sheets in your workbook.
Sheet1 (Sheet1)
Sheet2 (Sheet Name that User Sees Here)
The name in ()'s is the name of the worksheet--the name that the user sees on
the Tab.
The name before the ()'s is the codename.
If you select one of the sheets in the project explorer and hit F4, you'll see
the properties window.
The (Name) property is the codename.
You can change the codename to something meaningful to make your code easier to
read.
Worksheets(1).range("a1").value = "hi"
or
worksheets("Sheet1").range("a1").value = "hi"
can be replaced with the codename (say Prices)
Prices.range("a1").value = "hi"
"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
--
Dave Peterson
|