The true problem I'm trying to solve:
I've pasted text including formulas from one Excel workbook to another, but I don't want to reference the old workbook in the formulas
The question I'm asking:
How can I find and replace (or similar results) references to an old workbook, where I want formula to instead reference (identically named) internal data table? I want to replace the values within a formula, but my best guess is that Excel is having a problem with some kind of special character
Sample:
The following is formatted as a data table named "Data" in "MyOldWorkbook.xlsx" and "MyCurrentWorkbook.xlsx"
ColRowNum
ColAmt
1
5
2
10
3
5
The original/desired formula: =COUNTIFS(Data[ColAmt],"<=10")
The unwanted formula when pasted: =COUNTIFS('MyOldWorkbook.xlsx'!Data[ColAmt],"<=10")
Notes:
The sample is overly simplistic. The easiest thing to do is manually just deleted out the unwanted text, but I'm looking for the systematic solution.
I feel like I'm missing some "escape" character command/grep thing/something in the find & replace dialogue.
Fails
If I try to put into the Find and Replace dialogue box(es) Find: 'MyOldWorkbook.xlsx'! and replace with "" (actually blank), I get an error.
If I try to shorten it (remove the !) I get an error about how excel has broken formulas (true) and refuses to find/replace for me
Can't break the external links, since that converts my formulas (formula, in this simple example) to numbers
UPDATE I've encountered this issue before and didn't have a solution other than manually fixing the text. When I was replicating the problem to a colleague, the above find-and-replace method actually worked. I don't think this has ever worked for me before. Does anyone have any ideas????
Sort of not-a-terrible-Solution: Use a Formula
As I'm posting this, stackoverflow tried to see if my question was answered before and it got me thinking which got to a solution that isn't horrendous, though I was hoping to use the GUI find/replace feature:
A1 (formula): =COUNTIFS(Data[ColAmt],"<=10")
B1 (text): 'MyOldWorkbook.xlsx'!
Use this formula in C1 =SUBSTITUTE(FORMULATEXT(A1),B1,"")
Copy and paste-vales from C1 into D1
Done
Terrible (but relatively functional) solution: Paste As Text etc.
Format formula cell as TEXT (or paste formula in a cell formatted as text)
Find/Replace "=" with "" to remove Excel thinking it's a formula (or otherwise remove "=")
Find/Replace "'MyOldWorkbook.xlsx'!" with ""
stitch back formula in some way and copy text back to original formula cell
You can programmatically remove references to different workbooks in Excel formulas using either a macro or VBA script.
You can loop through all of the cells in a worksheet and modify the formulas to remove any references to external workbooks. I have created an example below:
Sub remove_external_references()
Dim cell As Range
Dim ws As Worksheet
Set ws = ActiveSheet 'Change to the desired worksheet
For Each cell In ws.UsedRange
If cell.HasFormula Then
cell.Formula = Replace(cell.Formula, "[", "")
cell.Formula = Replace(cell.Formula, "]", "")
End If
Next cell
End Sub
This script uses the Replace function to remove any square bracket characters "[" and "]" from the formula, which are typically used to indicate a reference to an external workbook.
You could of course change it anyway you want to, for example: cell.Formula = Replace(cell.Formula, "'MyOldWorkbook.xlsx'!", "")
Additionally, you can use the Workbook.LinkSources property to get the list of external links for a workbook, and use the Workbook.ChangeLink method to update the references.
I m new to Excel VBA. i need to do a vlookup in excel sheet with this vlookup formula:
=INT(IFERROR(VLOOKUP(A:A,[sheet.xls]Sheet1!B:F,5,FALSE),0))
Could any one guide me to apply this vlookup using VBA
Basic approach:
myRange.Formula = "=INT(IFERROR(VLOOKUP(A:A,[sheet.xls]Sheet1!B:F,5,FALSE),0))"
It could be worth that you give also a look at .FormulaR1C1 property which might be more explicit from VBA. If you don't use FormulaR1C1 syntax, pay attention to Absolute/relative address.
mySheet.Range("X2").Formula = "=INT(IFERROR(VLOOKUP($A:$A,[sheet.xls]Sheet1!$B:$F,5,FALSE),0))"
If you don't know how to do something with VBA, use the Macro Recorder. Its output is crappy and always needs to be cleaned up, but at leat you get an answer in 5 secs and it's an easy learning tool.
Eventually, enter the formula manually in a cell - say in X2 - then from the Immediate window, you type Print range("x2").Formula (or formulaR1C1) to see the exact formula to put in your code.
In Excel, A1 is equal to =B1/ BDP(C11&”Corp”, “ds036”)
Which BDP(Corp, ds036) is a function and parameters from Bloomberg.
How to use Excel VBA for this formula?
I was trying different ways in VBA to solve my point. One of them is like the line below,
Cells(1,1)=“B1/ BDP(C11&”Corp”, “ds036”)”
An other way I tried, to simplify,
For i=1 to10
Cells(i,1)=“Cells(i,2)/ BDP(cells(i,3)&”Corp”, “ds036”)”
Next
Also, if it can access directly to BDP function. That will be perfect!
try:
Cells(1,1).Formula = "=B1/ BDP(C11&""Corp"", ""ds036"")"
Note:
I used a different flavor of double quote
I doubled-up on the double quotes
Does this do what you want?
Range("A1:A10").Formula = "=B1/BDP(C3&""Corp"",""ds036"")"
If you assign a formula to a multi-cell range, I think Excel will automatically adjust relative cell references for subsequent rows/columns - similar to CTRL+D or dragging down.
This means you don't need to loop through each row and re-construct the formula string for each loop iteration.
I have this formula below that I am attempting to use to perform different calculations depending on the data in the referenced cells. The formula works, but I need to have more flexibility on the data found.
=IF(AND(U1="APO 300%",$E2="P+"),P2-(SUMIF($F$2:$F$74,300%,H$2:H$74)*M2)-(SUMIF($F$2:$F$74,150%,H$2:H$74)*M2),IF(AND(U1="APO 300%",$E2="P"),H2,""))
Is there a code that can duplicate this?
Worksheets have an Evaluate method which can be used to do what you want:
shtObject.Evaluate("A1+B1") '<<< substitute in your formula
How do I obtain a reference to the current cell?
For example, if I want to display the width of column A, I could use the following:
=CELL("width", A2)
However, I want the formula to be something like this:
=CELL("width", THIS_CELL)
Several years too late:
Just for completeness I want to give yet another answer:
First, go to Excel-Options -> Formulas and enable R1C1 references. Then use
=CELL("width", RC)
RC always refers the current Row, current Column, i.e. "this cell".
Rick Teachey's solution is basically a tweak to make the same possible in A1 reference style (see also GSerg's comment to Joey's answer and note his comment to Patrick McDonald's answer).
Cheers
:-)
Create a named formula called THIS_CELL
In the current worksheet, select cell A1 (this is important!)
Open Name Manager (Ctl+F3)
Click New...
Enter "THIS_CELL" (or just "THIS", which is my preference) into Name:
Enter the following formula into Refers to:
=!A1
NOTE: Be sure cell A1 is selected. This formula is relative to the ActiveCell.
Under Scope: select Workbook.
Click OK and close the Name Manager
Use the formula in the worksheet exactly as you wanted
=CELL("width",THIS_CELL)
EDIT: Better solution than using INDIRECT()
It's worth noting that the solution I've given should be preferred over any solution using the INDIRECT() function for two reasons:
It is nonvolatile, while INDIRECT() is a volatile Excel function, and as a result will dramatically slow down workbook calculation when it is used a lot.
It is much simpler, and does not require converting an address (in the form of ROW() COLUMN()) to a range reference to an address and back to a range reference again.
EDIT: Also see this question for more information on workbook-scoped, sheet dependent named ranges.
EDIT: Also see #imix's answer below for a variation on this idea (using RC style references). In that case, you could use =!RC for the THIS_CELL named range formula, or just use RC directly.
You could use
=CELL("width", INDIRECT(ADDRESS(ROW(), COLUMN())))
=ADDRESS(ROW(),COLUMN(),4) will give us the relative address of the current cell.
=INDIRECT(ADDRESS(ROW(),COLUMN()-1,4)) will give us the contents of the cell left of the current cell
=INDIRECT(ADDRESS(ROW()-1,COLUMN(),4)) will give us the contents of the cell above the current cell (great for calculating running totals)
Using CELL() function returns information about the last cell that was changed. So, if we enter a new row or column the CELL() reference will be affected and will not be the current cell's any longer.
A2 is already a relative reference and will change when you move the cell or copy the formula.
=ADDRESS(ROW(),COLUMN())
=ADDRESS(ROW(),COLUMN(),1)
=ADDRESS(ROW(),COLUMN(),2)
=ADDRESS(ROW(),COLUMN(),3)
=ADDRESS(ROW(),COLUMN(),4)
Without INDIRECT(): =CELL("width", OFFSET($A$1,ROW()-1,COLUMN()-1) )
I found the best way to handle this (for me) is to use the following:
Dim MyString as String
MyString = Application.ThisCell.Address
Range(MyString).Select
Hope this helps.
Inside tables you can use [#] which (unfortunately) Excel automatically expands to Table1[#] but it does work. (I'm using Excel 2010)
For example when having two columns [Change] and [Balance], putting this in the [Balance] column:
=OFFSET([#], -1, 0) + [Change]
Note of course that this depends on the order of the rows (just like most any other solution), so it's a bit fragile.
There is a better way that is safer and will not slow down your application. How Excel is set up, a cell can have either a value or a formula; the formula can not refer to its own cell. You end up with an infinite loop, since the new value would cause another calculation... . Use a helper column to calculate the value based on what you put in the other cell. For Example:
Column A is a True or False, Column B contains a monetary value, Column C contains the folowing formula:
=B1
Now, to calculate that column B will be highlighted yellow in a conditional format only if Column A is True and Column B is greater than Zero...
=AND(A1=True,C1>0)
You can then choose to hide column C
Full credit to the top answer by #rick-teachey, but you can extend that approach to work with Conditional Formatting. So that this answer is complete, I will duplicate Rick's answer in summary form and then extend it:
Select cell A1 in any worksheet.
Create a Named Range called THIS and set the Refers to: to =!A1.
Attempting to use THIS in Conditional Formatting formulas will result in the error:
You may not use references to other workbooks for Conditional Formatting criteria
If you want THIS to work in Conditional Formatting formulas:
Create another Named Range called THIS_CF and set the Refers to: to =THIS.
You can now use THIS_CF to refer to the current cell in Conditional Formatting formulas.
You can also use this approach to create other relative Named Ranges, such as THIS_COLUMN, THIS_ROW, ROW_ABOVE, COLUMN_LEFT, etc.
EDIT: the following is wrong, because Cell("width") returns the width of the last modified cell.
Cell("width") returns the width of the current cell, so you don't need a reference to the current cell. If you need one, though, cell("address") returns the address of the current cell, so if you need a reference to the current cell, use indirect(cell("address")). See the documentation: http://www.techonthenet.com/excel/formulas/cell.php
Reference to a cell that include this formula (self reference):
address(row();column())
E.g. getting the value of the cell above:
indirect(address(row()-1;column()))
Or what the OP asked:
=Cell(width;address(row();column()))