First post, please be kind ๐
I am updating a dataset by repeating calculations from a prior year (2015) onto the most recent year’s data (2016). I have copied the old year’s formulas into new columns, and I only have to change a few parts of each formula.
Example
2015 formula: =COUNTIFS(Data15[title], Datasheet!$B$2,
Data15[variable2015], “variable”, Data15[variable22015], “<=100”)2016 formula: =COUNTIFS(Data16[title], Datasheet!$B$2,
Data16[variable2016], “variable”, Data16[variable22016], “<=100”)
I only have to change two parts of the formula: Data15 -> Data16, 2015 -> 2016. There are hundreds of these formulas to update, and I could manually go in and update these small parts of the formula. It is much faster to Find and Replace parts of the formula for all the formulas at the same time. However – to my knowledge – Find and Replace only allows you to replace one thing at a time (i.e., either Data15 -> Data16 OR 2015 -> 2016). I have tried to replace one and then replace the other, but the formula breaks unless both of them are replaced together.
The question: In Excel, is there a way to find and replace multiple parts of a formula simultaneously? Essentially, I would like to replace both Data15 -> Data16 and 2015 -> 2016 in the same Find and Replace, for a batch of cells. Is that possible?
Thank you!
Here is a macro and instructions:
Sub FixFormulas()
Dim r As Range, rng As Range
Dim s As String
Set rng = Cells.SpecialCells(xlCellTypeFormulas)
For Each r In rng
r.Value = "'" & r.Formula
r.Replace what:="2015", Replacement:="2016"
r.Replace what:="Data15", Replacement:="Data16"
r.Formula = r.Value
Next r
End Sub
Macros are very easy to install and use:
- ALT-F11 brings up the VBE window
- ALT-I
ALT-M opens a fresh module - paste the stuff in and close the VBE window
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
- bring up the VBE window as above
- clear the code out
- close the VBE window
To use the macro from the Excel worksheet:
- ALT-F8
- Select the macro
- Touch RUN
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
Macros must be enabled for this to work!
EDIT#1:
Discard the previous code and use this instead:
Sub FixFormulas2()
Dim r As Range, rng As Range
Dim s As String
Set rng = Cells.SpecialCells(xlCellTypeFormulas)
Set rng = Intersect(rng, Range("Z4:AA120"))
For Each r In rng
r.Value = "'" & r.Formula
r.Replace what:="2015", Replacement:="2016"
r.Replace what:="Data15", Replacement:="Data16"
r.Formula = r.Value
Next r
End Sub
(we are only adding a single line of code)
Tags: excelexcel, replace