Can we batch rename excel variable names? - excel

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).

Related

I need to dynamically reference from one excel workbook to another

I have an excel workbook titled "Invoices" that contains invoicing templates for approximately 35 employees. Cosmetically, all of the sheets are the same, except for employer, pay rate, name etc. Each employee has a different sheet within this workbook.
In addition, I get weekly "Master Time card reports" for approximately 120 employees that I must download as a separate excel workbook weekly. Each one of these employees has a separate worksheet. The data from the "Master Time card reports" must be transferred to the templates on the "Invoices" workbook. I have been doing this manually, and have been trying to figure out a way in Excel to dynamically reference the sheets in the "Master Time card reports" workbook from the "Invoices" workbook.
The issue is that the cell addresses on the "Master time card reports" change, depending on a number of factors. One week, the total hours for any particular employee may be in cell "T50", and then the next week they may be in cell "T43" (they never change columns). Also, the names of the sheets may change from "Sheet1" to "Sheet3", because new employees are added, or roll off.
I have done an extensive amount of research and can't seem to find a way to do this.
This is the formula that I am using so far:
=INDIRECT("'[Mastetimecardreports.xlsx]*'!B"&MATCH($D18,[Mastetimecardreports.xlsx]Sheet1:Sheet129!$B:$B,0)+1)
My goal is to enter the name of an employee on their respective worksheet in the "Invoices" workbook and have that name be found in the "Master time card reports" workbook and pull data from the sheet for that respective employee. I want to do this dynamically, as the cell address and sheets will always be changing.
You may greatly benefit from a data architecture restructure. Whenever I read something like Each employee has a different sheet within this workbook, my alarm bells go off. Why would you do that? Why would you spread your data around multiple workbooks and inside those workbooks into multiple worksheets? This is the reason for your difficulty to aggregate the data. Use ONE workbook and ONE worksheet to store the data. If your data collection is in different files, Power Query can be used to create the ONE data file. Then you can easily create reports off that one data file.

In Excel, how do I reference a local worksheet within a template?

I am creating my own template which I need to use data from a worksheet called Schedules. The template contains 3 sheets, none of which are the Schedules sheet. This template will only be used in workbooks where the Schedules sheet already exists.
My question is, how do I reference a local worksheet which isn't in the template?
Whenever I create the template I reference the Schedules sheet but it asks for a file destination. How can I force it to look for a local sheet, even though the sheet won't exist until the template is actually used?
Depending on how complex your formulas, this could get messy pretty quickly, but one option would be to use the INDIRECT formula.
Some examples are below:
Gives you the value of A1 on the Schedules sheet
=INDIRECT("Schedules!"&"A1")
Looks up the ID '5' in the range A1:C20 in the Schedules sheet and returns the corresponding value in column B
=VLOOKUP(5,INDIRECT("Schedules!"&"A1:C20"),2,0)
Multiplies two values on the Schedules sheet
=INDIRECT("Schedules!"&"A1")*INDIRECT("Schedules!"&"A2")
Once you get your formulas working you can move your templates to the workbook that has the Schedules. All of your formulas will update with appropriate values, and for the record, calculations should be set to automatic.

Updating a workbook's existing structured table from an external master table?

Currently: I have a few sheets that contain one or more tables (e.g., Sheet1 has one table while Sheet2 has three tables). These same sheets are found in several end user workbooks. The tables in those sheets are used by structured references within cell formulas in the workbook.
Goal: I would like to have another workbook (a master workbook) that only has the sheets that contain the tables. Updates would be manually made to the master workbook i.e., add a row, delete a row, edit a row, add a column, delete a column, edit a column. Once updates are completed with the master workbook, the updated [master] sheets would then be placed into the various user workbooks, replacing the existing sheets and thus the tables (using VBA)...without breaking the structured references! No #REF errors! Well, this is what I'm envisioning, however, it doesn't have to be exactly this. Also, the common table sheets must be in the end user workbook, no external referencing! Unless it's part of the process to make what I need work, of course :-)
I'm very handy with VBA but not a MVP. Assume all of the workbooks -- both end user and master -- exist in the same directory. The master workbook would contain the code that would deliver the updated sheets to the end user workbooks. Excel 2007 & 2010 in Windows 7 environment.
I don't have any code as I'm trying to manually figure it out first without breaking anything. Suggestions welcomed! Thank you.
[Update 13Jun2012] Hopefully this more long-winded explaination will help.
What I'm doing:
I have a workbook that (essentially) copies itself into an end user workbook (1 to n times based on user form inputs). The end user workbook is protected to keep end users from making mistakes in certain formulas (various forms of sheet/cell protection). There are some sheets that contain tables and these tables are the same for all users. These tables are used for data validation (e.g., Allow: List; Source: =timing_droplist) and VLOOKUPs (e.g., =VLOOKUP($W8,Timing_table,FE$5+1,FALSE)). These end user workbooks are created once a year but updated three times during the year (they're for finance/budgeting). The tables can be updated at any time. Since data is added to the end user workbooks it's not convienent to re-generate those workbooks with the updated tables and then have the end user re-enter all their data. So it would better if the existing end user workbooks could have the common tables updated all together (using VBA).
What's happening (just ONE example):
"Timing" sheet exists and contains the table "Timing_table". If the master "Timing" sheet is added to the workbook, Excel renames it to "Timing (2)". This is normal. But the table in the added sheet becomes local instead of globle AND it, too, gets renamed, FOR EXAMPLE, "Timing_table12". I don't know how Excel comes up with the digits for the table name and they could change. If I delete the old "Timing" sheet all references to it are broken (of course) -- pulling up Name Manager shows #REF! in the value column. Renaming the new references doesn't fix the issue because the new references are local to the new sheet.
I tried manually cutting and pasting a table into another, pasting as Text, and that doesn't keep formulas (basically it performs a Paste as Values operation) and, overall, doesn't work. Just looking for options. External references to these tables is not an option.
I think that this thread might help you out: http://www.ozgrid.com/forum/showthread.php?t=66791
To provide you the reply directly (assuming that the problem mainly involves the external referencing):
Replied by Aaron Blood:
Re: Copy Formula To New Worksheet Without Path
You've already developed the code to do the copy/pasting to a new sheet and workbook. Now all you need to do is scan the formulas of the new worksheet and remove the ext refs from the formulas.
Run something like this at the end of your existing copy/paste macro...
VB:
Sub ExtRef_Remover()
Dim cell As Range, n As Variant
For Each cell In Workbooks("New_WB").Sheets("Sheet1").Cells.SpecialCells(xlFormulas)
n = Application.Find("]", cell.Formula)
If Not IsError(n) Then
cell.Formula = "='" & Right(cell.Formula, Len(cell.Formula) - n)
End If
Next cell
End Sub
Using extensive external references just didn't work out in this project so I convinced the project manager that we needed to change course. She agreed that what she wanted was too much to ask and taking too much time to implement, aside from compromising workbook generation and stability. I ended up using a data pull into the necessary tables via a command button. Waaaay easier to work with and stable.

how to select variables in same cells of multiple workbooks in Excel

I am running an experiment that is 8 weeks long and consists of about 100 subjects. There are variables from each week that I would like to select, but each subject has his own workbook. Is there a way to select the variables (which are the same cells in each workbook) all into one workbook? or can I do this with macro?
You can pull values from a cell in another workbook with a formula like this (assuming the other workbook is in the same folder):
=[Book1.xlsx]Sheet1!A1
Or you can specify the full path to another workbook like this (note where the single quotes go -- particularly the one after the sheet name):
='C:\Users\Brandon\Desktop\test\[Book1.xlsx]Sheet1'!$A$1
I think Excel automatically changes it to the full path style when you put it in the first way I mentioned. You can of course use the value in equations as well:
=6 + 5 * [Book1.xlsx]Sheet1!A1

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