Vba to set cell reference to absolute and back to original setting.

glynn1969

Board Regular
Joined
Nov 24, 2018
Messages
88
Office Version
  1. 365
Platform
  1. Windows
Hello trying to keep example simple here.

So in cells A1:A5 I have reference links to cells D1:D5.

I then have a macro to convert links to absolute values.

Sub test()
Dim c As Range
For Each c In Selection
c.Formula = Application.ConvertFormula(c.Formula, xlA1, , xlAbsolute)
Next


End Sub

Then a second macro to convert back,

Sub test()
Dim c As Range
For Each c In Selection
c.Formula = Application.ConvertFormula(c.Formula, xlA1, , xlrelative)
Next

End Sub

however when I convert back the cell references change eg the Reference in A2 becomes "=D3" instead of back to "=D2", the reference in A3 becomes "=D5" instead of back to "=D3" etc

Any advice on what I'm doing wrong
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
VBA Code:
Sub toAbs()
Dim c As Range
    For Each c In Selection
        c.Formula = Application.ConvertFormula(c.Formula, xlA1, xlA1, xlAbsolute)
    Next
End Sub

Sub toRel()
Dim c As Range
    For Each c In Selection
        c.Formula = Application.ConvertFormula(c.Formula, xlA1, xlA1, xlRelative)
    Next
End Sub
 
Upvote 0
VBA Code:
Sub toAbs()
Dim c As Range
    For Each c In Selection
        c.Formula = Application.ConvertFormula(c.Formula, xlA1, xlA1, xlAbsolute)
    Next
End Sub

Sub toRel()
Dim c As Range
    For Each c In Selection
        c.Formula = Application.ConvertFormula(c.Formula, xlA1, xlA1, xlRelative)
    Next
End Sub
Excellent thanks LazyBug... easy when you know how
 
Upvote 0
Or swith between relative/absolute. single sub
VBA Code:
Sub switch()
Dim c As Range
For Each c In Selection
    c.Formula = Application.ConvertFormula(c.Formula, xlA1, xlA1, IIf(InStr(c.Formula, "$"), xlRelative, xlAbsolute))
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top