简体   繁体   中英

Variable name of a worksheet

I want to name a worksheet in excel as the value inside cell "C6" in the tab named "Control". I am new to VBA and what I tried was typing this on a module.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'
' Macro2 Macro
'

'
    Dim month As String
    month = Sheet2.Range("C5")
    Sheets("Month").Name = month
End Sub

Moreover, I do not know whether the name will be updated automatically. I do not want to have to run a macro to change the worksheet name...

Thanks!

It sounds like you want to be able to always refer to the same worksheet, regardless of the worksheet's name changing (as shown on the worksheet's tab) .

This is where the worksheets' CODENAME Property comes in handy.

Let's say you have an object variable declare for your worksheet like Dim ws As Worksheet . You can refer to a worksheet three basic ways.


...by Name :

Set object variable:

 Set ws = Sheets("Sheet1") 

The worksheet name is the only worksheet identifier that can be changed to whatever you want, which (as you're probably aware) is done like:

 ws.Name = "NewSheetname" 

...or alternatively, like:

 Sheets("Sheet1").Name = "NewSheetName" 

...by Index Number :

The index number identifies the position of the worksheet's "tab" , compared to the others (and cannot be changed without changing the order of the worksheets)

Set object variable:

 Set ws = Sheets(1) 

...then you could (still) change the worksheets name like:

 ws.Name = "NewSheetname" 

...or you could change the name by referring to the worksheet index number like:

 Sheets(1).Name = "NewSheetName" 

NOTE: If the worksheet is moved (or another worksheet is inserted before it), the Index number will change! Therefore, it's usually not the preferred method of referring to a worksheet.


...by CodeName :

The CodeName is how Excel refers to the worksheets internally. It is the original name that Excel gave the worksheet, and it does not change since it is a read-only property.

Set object variable:

 Set ws = Sheet1 

...then you could (still) change the worksheets name like:

 ws.Name = "NewSheetname" 

...or you could change the name by referring to the worksheet codename like:

 Sheet1.Name = "NewSheetName" 

You can check a worksheets' CodeName property like:

 MsgBox Sheets("YourSheetName").CodeName 

...or, if ws is already referencing the worksheet:

 MsgBox ws.CodeName 

So, in your case, you could change the name of your worksheet as often as you like, with:

Sheet2.Name = "NewNameHere"

...just keep referring to it as Sheet2 .


One more example to clarify the difference:

  • Create a new workbook. (It will automatically have worksheet named Sheet1 .)
  • Change the worksheet name to "Sheet1999", either manually (by double-clicking the name on it's tab) or programmatically (with Sheet1.Name="Sheet1999" )

  • Now, if you want to find out how many rows on that sheet have been used, you use use either:

     MsgBox Sheets("Sheet1999").UsedRange.Rows.Count 

    ...or:

     MsgBox Sheet1.UsedRange.Rows.Count 

A note about Sheets versus Worksheets :

When referring to a worksheet Sheets and Worksheets can usually be used interchangeably, so for example these two lines do the same thing :

Worksheets("mySheet").Calculate

Sheets("mySheet").Calculate

The difference is that:

  • the Worksheets object searches the Worksheets Collection for a matching Name , Index , or CodeName .

  • the Sheets object searches the Worksheets Collection **and the Charts Collection** for a matching Name , Index , or CodeName .

Therefore, the only time it would be a problem is if you have a chart and a worksheet with the same name .

So, don't ever name a chart the same as a worksheet , and then you won't have to worry about it, and can go on saving 4 keystrokes any time you refer to a Sheet ... :)


More Information:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM