Tuesday, February 15, 2005

Sheet names in VBA code

Stino asked me about how to use the sheet name in VBA macro code after I posted about how to use sheet names in the file.

If your macro code has selected a cell or a sheet location then the name of the sheet can be located by using the Name object for the sheet thus Activesheet.Name.
If you are using code that is accessing the entire collection of worksheets (for example the password protection code I supplied previously) then the same object applies but the syntax is different.

To cycle through the sheets and use their names use this code.

For Each ws In Sheets
x= ws.Name
Next ws