How to find the index number of renamed sheets in excel-vba - excel

I have about 8 sheets in a workbook with different names. Is there a way using VBA to activate one ore more of these sheets based on their index number? For example I have sheets named, "Month", "Name", "Age" etc... how can I find their index number?

Try this
Sheets("<sheet Name>").Index
If you want to get code name
Sheets("<sheet Name>").codename
It’s possible for the sheet name to be different from the code name. When you create a sheet, the sheet name and code name are the same, but changing the sheet name doesn’t change the code name, and changing the code name (using the Properties window in the Visual Basic Editor) doesn’t change the sheet name.

To get a sheet's index:
sheets("month").index
To activate by index:
sheets(5).activate

Related

Reference or second name to identify a group of worksheets

I want to add a second name (but hidden) to each Excel sheet.
Each group of worksheets is related to a different project.
Can I add the project name with VBA? I have two Listbox where you can choose a project name and I want to retrieve all the worksheet names related to the selected project.

Creating a hyperlink for a excel sheet: xlsxwriter

Is it possible to create a hyperlink for a specific sheet of an excel file?
I want to open a sheet on the click of a cell which is on another sheet of the same excel file. For example, if someone clicks on 'A1' cell which is in the sheet2 the sheet1 will be opened and both the sheets are in the abc.xlsx file.
You can do this with the XlsxWriter Worksheet write_url() method using the internal: URI. See the XlsxWriter docs on write_url().
I was unable to get this to work using the "write_url(A1, "internal:'sheet name'!A2")" form. Can someone provide some guidance on this?
I was able to successfully add hyperlinks to internal cells using the form:
write('A1', '=HYPERLINK(CELL("address", 'sheet name'!A2), "Friendly Name")
NOTE: the word 'address' is literal/not a generic reference, and, the quotes need to be specified as shown (i.e., single quotes for a multi-word sheet name, and double quotes for the word 'address' and the 'Friendly Name'...
To put a "Friendly Name" in the hyperlink use the string argument of write_url() method. For example, I did the following after setting the variable sheet_name, which in this case is both the name of the sheet to link to and the friendly name:
write_url(row, col, f"internal:'{sheet_name}'!A1", string=sheet_name)

How to refer a series a closed excel workbooks from an open excel work sheet

I have a series of files save with names A,B,C,D. These are the items of my first row in the sheet I am working in. Now I need to refer the data from file'A' when I am working on the row that has 'A' in the first column.
I wanted to see if there is an option like VLOOKUP for different excel workbooks?
Thank you
Naga
You have to reference the file location, file name and the sheet name in order to do this.
Use the syntax in the example below;
=VLOOKUP(F3,'C:\Users\johndoe\Desktop[test1.xlsx]Sheet1'!A1:A6,1,FALSE)
Then Excel will open a file explorer box and have you select the correct file. Select it and then your formula should work.
See this link for more information:
https://support.office.com/en-us/article/Create-an-external-reference-link-to-a-cell-range-in-another-workbook-c98d1803-dd75-4668-ac6a-d7cca2a9b95f

Referring to dynamic worksheets names

is there any way I can use Sheets INDEX instead of NAME to address it?
For example I have
Sheet1 (Calculations)
How can I address this sheet instead of using:
='Calculations'!CELL
I need it as sheets names will be changing according to their values
This link provides a way to do it with a user-defined function in VBA:
referencing sheets by number instead of name in cells
However, you may also want to consider a method that does not change the sheet names. As #DirkReichel implies, you can change sheet indices without changing sheet names and vice versa.
You might try using summary information stored on the sheet in question or keeping a summary sheet that references each sheet.

Excel VBA worksheet.names vs worksheet.range

I have created a defined name/range on a worksheet called bob, pointing to a single cell. There are a number of other name/ranges set up on this worksheet, which I didn't create. All the number/ranges work perfectly except for mine.
I should be able to refer to the contents of this cell by using either of the following statements:
(worksheet object).Names("bob").RefersToRange.Value
(worksheet object).Range("bob").Value
However, only the second statement, referring to the Range works for some reason. The first one can't find the name in the Names list.
My questions are:
What is the difference, if any, between a Name and a Range?
Is this something to do with the global/local scope of my name/range?
How were the other name/ranges created on the sheet so that they appear in both the worksheets Name and Range list?
Yes, you are right.
Names can be local (belong to a worksheet) and global (belong to a workbook).
(worksheet object).Names("bob") will only find a local name. Your name is obviously global so you could access it as (worksheet object).Workbook.Names("bob").RefersToRange.
The "other names" are probably local. They only appear in the ranges list when their parent sheet is active (check that out). To create a local name, prepend it with the sheet name, separated by a '!': 'My Sheet Name'!bob.
I don't know how to do it with code, but if you go to the Name Manager Under the Formula tab group in the Ribbon in Excel 2007, you can create names and choose their scope.

Resources