Add a formula to column immediately after a range of values.

NeilATaylor

Board Regular
Joined
Aug 7, 2007
Messages
185
Hi,
An extremely simple task (I'm predicting) but very frustrating for a rookie.
How do I add a Macro that will add an =ABS() formula to the last column of a range of values, and then sort the range (including the new ABS value) by the ABS value?
Thanks for any help offered
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Boller

Banned
Joined
Apr 11, 2006
Messages
2,328
Provided that the range consists of one area only, and assuming that you want to replace the values in the last column of the range :-

Code:
Dim rng As Range, lc#
Set rng = [B2:F50] 'or whatever
lc = Range([A1], rng).Columns.Count
Columns(lc).Insert
With Intersect(Columns(lc), rng)
    .FormulaR1C1 = "=ABS(RC[1])"
    .Offset(, 1).Value = .Value
    .EntireRow.Sort Cells(1, lc + 1), Order1:=xlAscending, Header:=xlNo
End With
Columns(lc).Delete
 
Upvote 0

NeilATaylor

Board Regular
Joined
Aug 7, 2007
Messages
185
thats awesome boller, thanks! How would I keep the original values (in the last column) with the ABS value to the right i.e. not replacing the original value?
 
Upvote 0

Boller

Banned
Joined
Apr 11, 2006
Messages
2,328
Code:
Dim rng As Range, lc#
Set rng = [B2:F50] 'or whatever
lc = Range([A1], rng).Columns.Count
With Intersect(Columns(lc + 1), rng.Offset(, 1))
    .FormulaR1C1 = "=ABS(RC[-1])"
    .EntireRow.Sort Cells(1, lc + 1), Order1:=xlAscending, Header:=xlNo
End With
 
Upvote 0

Forum statistics

Threads
1,191,547
Messages
5,987,209
Members
440,085
Latest member
MBecker79

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
Top