Excel VBA worksheet.names vs worksheet.range - excel

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.

Related

Can we batch rename excel variable names?

Image you have an excel sheet, with names as variables for formulas, references, etc. (all viewable the Names Manager).
You want to create clones of this sheet, and add them as separate sheets to another workbook.
However, the names will conflict.
E.g. FirstColumnTitle will be the same for each sheet.
Excel seems to latch the basic function to batch rename variable names in the Names Manager, so the same name can be renamed (e.g. append ‘_sheet1’ to FirstColumnTitle making it ‘FirstColumnTitle_sheet1’).
How would one go about this?
I tried Names Manager, and a VB Macro (link below) that was supposed to work (but only renamed a single variable).
Sub RenameCells()
Dim n As Name
For Each n In ThisWorkbook.Names
n.Name = "KUTOOLS_" & n.Name
Next n
End Sub
I can manually change the names one by one, but… what if there are hundreds of names on a sheet that will be cloned to the same workbook a handful of times?
Example: the sheet/worksheet is a loan amortization template. In the Template, it has names such as “LoanAmount” which refers to the cell where the loan amount is entered, and “LoanInterestRate” referring to the cell sign the APR.
If I try to copy that whole worksheet into another workbook twice (two different copies of the loan worksheet in two tabs in one workbook) the variable names (found in Names Manager) conflict with one another and end up interfering with one another or blocking calculations/functionality in one or the other worksheet (if they have different values.)
How to reproduce the problem (that I'm trying to solve):
Create a new spreadsheet from the "Loan Amortization Schedule" template,
then create a copy of that sheet a couple times to the same workbook (or another).
Now set a loan balance, APR, loan term, and payments per year (make it different for each sheet such that the number of payments should be different).
Notice that in the second (and third and fourth) sheet, the result for "Actual Number of Payments" for each sheet is whatever the value is for the first sheet. Open Names Manager, and the name for each indicates that its scope is only for the appropriate sheet, yet somehow sheet1's ActualNumberOfPayments reference name is referred to in the other sheets (even though their respective ActualNumberOfPayments appropriately refers to the other reference names in their own sheet only).

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.

Dynamically referencing a cell

I would like to set up Names for the drive, file path, Excel spreadsheet name and sheet name in cells in Excel, and then concatenate them together when referencing external cells. This will allow me to create macros that reference sheets dynamically in the sense that it does not matter where the spreadsheet is.
So far, I have the following:
Drive = C
Location = Users\ashleys\Desktop\New Quote Production\Quote Location 3\Ash
Name = Book3
Sheet = Sheet1
(Please note that the = sign above can be read as "assigned Name as")
I concatenate these together to create another Name (called Final) in the following fashion:
Final = "'"&Drive&":\"&Location&"["&Name&".xlsx]"&Sheet&"'!"
Final resolves to be the following:
C:\Users\ashleys\Desktop\New Quote Production\Quote Location 3\Ash\[Book3.xlsx]Sheet1'!
So, as far as I am concerned, so far so good!
However, I am embarrassingly stumbling at the last hurdle. when I use the Final defined Name in cell references, it does not resolve in the way I expect. So, if I am in Book1 and I use the code above to refer to cell A3 (which for example contains the word "Success!") in Book3, I would hope that typing the following into a cell in Book1 would resolve to "Success!":
=Full&A3
Instead, the A3 resolves to A3 in Book1.
You'd need to use INDIRECT for that to work, but the other workbook would have to be open.
http://www.cpearson.com/excel/indirect.htm
As the workbook has to be open for it to work there's no need to add the file location to the string. It may be useful in some VBA code used in the Workbook open event to automatically open the other workbook though, or look at Excel workspaces to open the other files.
http://www.ozgrid.com/Excel/save-as-workspace.htm
i.e. =INDIRECT("'[Book3.xlsx]Sheet1'!$A$1") or =INDIRECT(Final) if Final is a named value.

Why can't Excel update some of the links in my workbook

I have a Master workbook in MS Excel that references data in 4 other workbooks.
When it opens I get the following message:
We can't update some of the links in your workbook right now.
You can continue without updating their values, or edit the links you think are wrong.
[Continue] [Edit Links...]
Each referenced workbook contains a N row, 365 column rectangular range that I have named 'DataRange'
My master workbook contains 4 lots of N row, 365 column ranges with Array Formula linked to these named ranges:
{='C:\Path\[Workbook1Name.xlsx]Sheet'![DataRange]}
It feels like i've come across an Excel bug.. Anyone know what I'm doing wrong?
Are you using MS Excel 2013, when you receive this error?
If so, it maybe due to the "3D reference in your named DataRange" in Excel 2013 or because you're referencing a range or cell in a closed workbook.
Excel allows the defining of names that refer to specific cells or ranges of cells in that workbook, similarly (using the Define Name tool in the Formulas tab) you can assign a formula to a name and then use that name in place of the formula.
The named formula or named data range is part of a collection in that workbook's (workbook1) objects and can be used in different workbook (workbook2), but the link to the defined name in the other workbook must be done one of two ways.
Link the defined name with a formula like this
='C:\Folder\Path\Workbook1.xls'!NamedFormula or NamedDataRange
Or Create a defined name in workbook2 (it can have the same defined name as workbook1's or a new name) and then refer workbook2's defined name to workbook1's defined name. Select Define Name on the Formula tab and copy a link like this in the Refers To Field:
='C:\Folder\Path\Workbook1.xls'!NamedFormula or NamedDataRange
Note these links will only work when being used in simple formulas which treat the link as a direct links. It will not work with more complex formulas when the original workbook (workbook1) is closed because the link is often treated as an indirect link in complicated formulas.
To work around this issue, use either of the following methods:
Save workbook1 and workbook2 as .xlsb files.
Do not add an external reference link to a defined name that refers to a 3D reference or named range.
Or open both workbooks
For more detail information, please refer to the following link:
https://support.microsoft.com/en-us/kb/2755120
If you're still receiving an error/ warning message after trying all my above suggestion. You might want to some troubleshooting.
Try this:
- Open Excel. Under Tools->Options->Calculation, select the Update remote
references box.
Open your workbook. Make sure the box the remote reference box is checked. Save your workbook this way after updating the link. Close workbook and Excel.Restart Excel and open your workbook.
- Look under defined names and delete all that you don't need or that have broken formulae. Run a FindLink search, to pin point the problem.
This process worked for one of workmates with this exact problem.
I hope this helps, if not let me know. There might be a 4th solution.
I finally solved this issue after going on a scavenger hunt. In my case, the reference was to an old file that no longer exists, and removing the reference was all that was needed. Unfortunately Excel does not pinpoint where the reference is, so it was a pain to look for the cell that was making the reference.
In my workbook, it was a reference as a part of a Data Validation. For others seeing the same error, I recommend using the Find & Select -> Go To Special feature (Home ribbon tab) then searching by Formula errors and and Data validation.
Open Excel
Go to "Inquire" tab page in the ribbon
Select "Workbook Analysis"
Check Items\Cells\With Validation Criteria items. In the "Validation Text" you might spot references to the extenal source.
The "Cell Address" column would give you the list of problematic cells.
Look around any other supsicious findings of the Workbook Analysis tool
Have you considered giving the data ranges different names? DataRange1, DataRange2 etc, or is this not possible due to other constraints. When excel says "some" of the links, how far has it got?
I got it working by ditching the FormulaArray and set the FormulaR1C1 for the entire range. Unfortunately the formula was a bit more complicated.
=INDEX(
'C:\Path\Workbook1Name.xlsx'!DataRange,
ROW() - ROW(Workbook1DataRange) + ROW('C:\Path\Workbook1Name.xlsx'!DataRange) - 5,
COLUMN() - COLUMN('C:\Path\Workbook1Name.xlsx'!DataRange) + 1)

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

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

Resources