Excel allows you to easily edit formulas. In doing so, you can quickly change a cell reference or a range reference from relative to absolute. What if you have a large number of cells in which you need to change from relative to absolute referencing? In this instance, the nature of the problem is well-suited to being solved through a macro.
By using the ConvertFormula method available in VBA, you can easily convert a formula from relative to absolute addressing. The following short macro uses this method to change the addressing method used in a range of cells:
Sub Relative2Absolute() For Each c In Selection If c.HasFormula = True Then c.Formula = Application.ConvertFormula(c.Formula, _ xlA1, xlA1, xlAbsolute) End If Next c End Sub
The key to how this macro works is, of course, in the ConvertFormula method. The last parameter used by the method is—in this case—xlAbsolute. If you want to adapt the macro so that it changes to other types of addressing, you can change xlAbsolute to xlRelative, xlAbsRowRelColumn, or xlRelRowAbsColumn. (I'm sure you can figure out the purpose of each constant by its name.)
There is one other thing to remember in regards to the ConvertFormula method: It has a length limit of 255 characters. That means that if your formula is very long, it is possible that the method won't work as you'd like. The best way to figure out if it will work for your needs is to test it out.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10738) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Converting from Relative to Absolute.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
Adding row numbers to a column of your worksheet is easy; you just need to use a formula to do it. Here's a quick look at ...
Discover MoreThe FIND and SEARCH functions are great for finding the initial occurrence of a character in a text string, but what if ...
Discover MoreGiven a range of cells containing values, you may have a need to find the first value in the range that is unique. This ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2019-08-22 12:03:17
Peter Atherton
To those concerned
The following macro is more flexible and the one I use.
Sub ConverFormulaReferences()
'Updateby20140603
'Extended Office
Dim Rng As Range
Dim WorkRng As Range
Dim xName As Name
Dim xIndex As Integer
Dim xTitleId As String
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Set WorkRng = WorkRng.SpecialCells(xlCellTypeFormulas)
xIndex = Application.InputBox("Change formulas to?" & Chr(13) & Chr(13) _
& "Absolute = 1" & vbLf _
& "Row absolute = 2" & vbLf _
& "Column absolute = 3" & vbLf _
& "Relative = 4", xTitleId, 1, Type:=1)
For Each Rng In WorkRng
Rng.Formula = Application.ConvertFormula(Rng.Formula, XlReferenceStyle.xlA1, XlReferenceStyle.xlA1, xIndex)
Next
End Sub
A big thanks to Extended Office
2019-08-20 13:33:05
Angela
You're my go to whenever Excel stumps me. I am most grateful, by far, for this tip! I know little about Macros, but followed all of the related instructions and updated more than 400 formulas in under a minute. I cannot thank you enough!
2019-08-06 06:52:46
Daniel
you just saved me about an hour of work, thanks so much!
2017-05-05 20:04:51
Prabir Mittra
Thank you.
I am a intermediate excel user. I found this tip fantastic as i avoided converting from Relative to Absolute by manually using F4 in each cell.
Prabir Mittra
Malaysia
2016-12-13 11:41:38
Hannes Koppatz
Dear all,
I've coded
myCell.Value = Application.ConvertFormula(myCell.Formula, xlA1, xlA1, xlAbsolute)
and it goes wrong, but
myCell.Value = Application.ConvertFormula(myCell.Formula, xlR1C1, xlA1, xlAbsolute)
works well.
Please change the second parameter of your method-call.
By the way: A general switch of R1C1 to A1-style or vice versa is not a solution for me.
I've implemented a SQL-Mechanism (by ADO) for Worksheets in combination of udf's.
Example: SELECT Name, '=udfTrim(RC[-1])' as Formula1 FROM ...
and these relative formulas has to be converted "on the fly" to absolutes.
And it works fine !
best regards !
2016-11-19 05:46:08
Steve
Efren,
In B4 you need =sum(A1:A3)-B5
2016-11-18 06:53:20
Efren
Sir Good Day, How can I automatically Sum Up the total values of a Column to the next column by not making prompt the summation formula, with that formula I may just copy to the rest of the rows to do the same task. To be clear let say I encoded Column A from Cell 2 to 3 wit Amounts of 5,10 and 6 Respectively, I want it to be copied the sum of 21 automatically to Column B at Cell4, and once put a value in column B at Cell5 the Total of 21 will be lessened by the value I put in Cell5. Thanks and Good Day
2016-11-18 02:54:44
John Wilkinson
Hello,
Your macro did not work.
However when I added:
Dim c As Variant
to it, problem solved.
Audrey mentions ASAP Utilities - seems as though that is an add on that one must purchase!
2015-07-23 10:39:06
ExcelGeek Audrey
There is a much simpler way to change a range of cells without running a macro. Select the range you'd like to change, Go to ASAP Utilities and select "Change formula reference style". Here you can select which parts or all of the formulas to change to absolute referencing. :)
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments