change link file path in excel - vba

I would like to know if it is possible to change the file path of a link in excel, using text in another cell. The trick here is that the source workbook is not open at the same time as the one that has the link in it, and is not located in the same folder.
ie: if once cell has a link like:
='C:\thanks\forthehelp\01\[eg.xlsx]worksheet'!A1
and I want to change the file based on text input from a cell, ie: cell A1 has some text: "02"
='C:\thanks\forthehelp\&A1&\[eg.xlsx]worksheet'!A1
to get
='C:\thanks\forthehelp\02\[eg.xlsx]worksheet'!A1
I know the above example doesn't work, but i think it illustrates what i want to do here. Any help would be really appreciated. I hope this makes sense.
Thanks.

You could use the INDIRECT function to implement a constucted string to the destination workbook but INDIRECT does not work on closed workbooks.
A VBA routine could replace the formula in the cell.
Range("A1").Formula = Replace(Range("A1").Formula, "\01\", "\02\")
If you opt for this sort of formula replacement, you should check to ensure that the external workbook exists in the new location with something akin to the Dir function. Using the Range.SpecialCells method with the xlCellTypeFormulas property would speed up going through the cells as opposed to looping through all of the cells and determining if the Range.HasFormula property is true.

Related

Named Cells and Formulas In Excel

How do I utilize named cell references in Excel that aren't absolute. I want to be able to take a formula and be able to drag it across excel and have one name cell reference update to a different named cell as I move across.
For example: I want to keep RevenuePerStay going across the formulas row and have excel updated the cell reference to the number of people staying. So
400 should be RevenuePerStay * Stay400
600 should be `RevenuePerStay * Stay600`
I tried using mixed cell reference and relative cell references using the dollar sign but excel will not accept this.
Assuming your stays are in row 5:
For this worksheet, click on cell C7 and go to create a new named range called Stays and for the formula write =C$5$ and exit the name manager.
Now change your formula in C7 to being RevenuePerStay*Stays and drag it across. This will get the right amount of stays you want each time.
In explicit answer to your question: no you would never get the name in the formula to change unless you put all scenarios in the formula using multiple if statements.
If I understand your question correctly, this method seems convoluted because you can use =RevenuePerStay*C5 and drag over the row, and it should give the answer you want.
If you really want to take the advantage of named range and make it change dynamically, you will need to incorporate with INDIRECT like this:
=RevenuePerStay*INDIRECT("Stay"&C5)
But this is assuming you have all the named ranges defined properly such as Stay200, Stay400, Stay600, Stay800, Stay1000 like below. Otherwise it will not work.

Is it possible to have a cell that has a formula and accepts entry at same time in excel?

Example:
A B
1 =vlookup(XX)
2
3
in cell A1 there is a Vlookup formula, Is it possible to enable user entry in this cell and override the formula then later restore the formula automatically when sheet is open again?
Even through VBA
Short, boring answer: nope.
A cell only ever has a keyed-in value, or a calculated formula. Can't have both.
Longer answer: maybe.
Shift everything 1 row down, and use row 1 to store your "original" formula - then hide that row (and pray the user isn't going to mess with it).
When the sheet is opened again sounds like you're confusing "workbook" and "worksheet" - you need to handle Workbook_Open if you want to run code when a workbook opens. Workbooks contain worksheets - it's the workbook that opens, not the sheets (sheets activate, but I doubt you would want to put that logic in there).
So, in the handler for Workbook_Open, write code that takes the formula in the hidden row and overwrites whatever is under it.
Another solution can be to hard-code the formula in the VBA code.
One possibility would be to store your Workbook as a template. Normally when a user opens the workbook by double-clicking, it will open whole new workbook based on the template, and they can modify it to their heart's content, save it, mail it to Grandma, etc.
The next person who comes along will double-click the template file and get the formula again, just as you designed it.
Short answer: Kind of, sort of
Long answer:
Save your workbook as a template. Every time someone will use it you'll see the orignal with formula, then if someone write over the formula, when using save your original will be kept intact.
What You need to do is:
press Alt + F11
select ThisWorkbook and paste this code:
Private Sub Workbook_Open()
Worksheets("Sheet1").Range("A11").Value = "asdf"
End Sub
Every time the workbook is opened, this script will run.
Instead of "Sheet1" you can write the name of the sheet you want to apply the script.
Inside the Range, You can define the cells you want to modify, You can use even multiple cells. Check this for more information about this.
After Value You can write what You want to be written inside the cell. You can write "=vlookup(XX)" and it will work.

How to link files in excel with a text in the path changing dynamically?

I have a file which needs to fetch data accordingly every week.
Let us say the formula to get data in a cell is
=\T:\Datafile\weekdata\2015\Week01\[Summary.xlsx]Sales'!D4
I have a cell in the sheet C3 which changes every week accordingly.It will change to week02 next week and i wish the path to change too..to
=\T:\Datafile\weekdata\2015\Week02\[Summary.xlsx]Sales'!D4"
I tried doing a concatenation to make the path dynamic
="\T:\Datafile\weekdata\2015\"&C3&"\[Summary.xlsx]Sales'!D4"
but it doesn't seem to work out.I checked evaluate formula and it resolves C3 to Week02 but the value doesn't come in the cell.In stead just the below text
\T:\Datafile\weekdata\2015\Week02\[Summary.xlsx]Sales'!D4
appears in the cell instead of any number.
Let me know where am i going wrong and how to resolve it.
Try =INDIRECT():
=INDIRECT("\T:\Datafile\weekdata\2015\"&C3&"[Summary.xlsx]Sales'!D4")
This function does exactly what you are trying to do, takes a built string and makes it a cell reference.
The problem with =INDIRECT() is that it only works if the source workbook is open:
If the source workbook is not open, INDIRECT returns the #REF! error value.
(That quote is from https://support.office.com/en-sg/article/INDIRECT-function-21f8bcfc-b174-4a50-9dc6-4dfb5b3361cd)
By googling "excel convert text to formula" I found this SO Q&A: How to turn a string formula into a "real" formula
If you don't like the VBA solution, I had success with
iDevlop's =EVALUATE() solution.

Excel Reference External Sheet

An excel question for you gurus. I've tried searching high and low and haven't come up with an effective solution.
I'm trying to create a formula that will lookup a value in an external sheet. I'm using the SUMPRODUCT formula and it works perfectly. Formula is below:
=SUMPRODUCT(--('File\Path\[file.xlsx]SheetName!$D$1:$D$1000=$B3), --('File\Path\[file.xlsx]SheetName'!$O$1:$O$1000=$A3), 'File\Path\[file.xlsx]SheetName'!$Q$1:$Q$1000)
The issue I'm running into, however, is that the source file is updated every day. Although the workbook name stays the same, the sheet name changes. A random string gets assigned to the source sheet name each time it is updated. As such SheetName becomes SheetName ase341.
Is there a way to have the formula read the external sheet number instead of the name? I want the formula to update regardless of the sheet name. If there's no way to read the sheet position is there a way to change the sheet name via a formula in an external workbook?
Usage Example
I have a workbook (analysis) and it pulls data from another workbook (source). Source is updated every day with new data. The data in Source is updated by downloading a report from the internet and saving over the old source file. As such, the file name stays the same but whatever is inside the file is always different (including the sheet name). There is always only ever one sheet in the Source with the same number of columns, always in the same position.
There is a really neat way to refer to a block of cells in an external workbook in which the sheetname or even the block address may vary. Say we have:
=SUM('C:\Users\James\Desktop\[Book1.xlsx]Sheet1'!$B$2:$B$9)
however the sheetname may vary. First assign a Defined Name to the block in Book1 (say XXX)
Then we can use:
=SUM('C:\Users\James\Desktop\Book1.xlsx'!XXX)
It does not matter if the sheetname changes, the Defined Name will change with it!
Your issue would be most efficiently solved with VBA, but if you're just getting started this might not be the best route.
You can get the sheetname or filename with just a formula, though:
http://www.ozgrid.com/VBA/return-sheet-name.htm

Cell reference in an Excel Macro

I have a macro to write, quite simple.
It just pulls numeric values from a Database and pops them into certain cells.
Problem is, I want the layout and design of the worksheet to be able to be changed without a care of the underlying macro. So obviously the cell references will change, for the cells that I need to populate with data.
Is there a way to mark a cell - say with "VALUE1" - in the background, and then reference that cell by using "VALUE1" - without needing to know its exact Cell position? So that its value can be updated - wherever it is on the Work Sheet?
Is there a TAG property or something that could be used? Although a function would have to be written to search through all the TAGs of every cell, but that is OK.
Any ideas?
I think this could be a bonus for any Macro developer :)
Office Documentation: Define and use names in formulas.