setting code sheet name

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Setting the code sheet name

Setting the code sheet name

resolvedResolved · Medium Priority · Version 2007

Rishi has attended:
Excel VBA Advanced course

Setting the code sheet name

I like to refer to sheets using the "(Name)" property of the sheet which is set in the VBAproject window. E.g. can set to shtEmployee and then reference the first cell for example by shtEmployee.range("A1")

The advantage of this is that if a user changes the name of the sheet in Excel, all the VBA code stil works.

However, when creating a new sheet with VBA I don't know how to set this name property to something which I can then refer to later.

RE: setting the code sheet name

Hi Rishi, thanks for your query.

IF you don't specific the name of the sheet - say by just using Range("A1") instead of Sheets(shtEmployee).Range("A1) - the code will run on whatever sheet has the focus. In order for you to work on a specific sheet, however, even if the sheet has been renamed, you'll need to have a unique identifier on that worksheet. For example, a particular heading or code. Then you loop through the workbook finding that unique identifier, set shtEmployee to be equal to the name of that found sheet and then run your code as normal. Fiddly, but it can be done.

Hope this helps,

Anthony

RE: setting the code sheet name

Hi,

When in the Excel Objects Screen it shows you the sheet name property that has been allocated to all sheet objects, this stays unique even when the worksheet is renamed.

So surely you could name these via the properties and then call them that way?

Kind regards,
Craig

Edited on Wed 19 Jan 2011, 12:31

RE: setting the code sheet name

Forgot to mention that everytime you add a new sheet to the workbook, it will be given the next available object sheet number of if a sheet has been deleted previously it will take that number as it's object Name. So you should be able to work out looking at your object window what the new object name will be using this theory.

Doing a sheet count should also get you the last object sheet name, if again you have no gaps in your sheet numbers due to deletions.

The code to do this is as follows:-
----
Sub check()
Sheets(Sheet2.Name).Select

End Sub
----
Sheet2 = the object sheet name.

Hope this helps.
Craig

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Hiding and unhiding columns using the keyboard

CTRL + 0 hides your columns and CTRL + SHIFT + ) unhides them although you would need to highlight the column letters either side as per normal

View all Excel hints and tips


Server loaded in 0.08 secs.