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

#### NeilATaylor

##### Board Regular
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
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``````

#### NeilATaylor

##### Board Regular
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?

#### Boller

##### Banned
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``````

Replies
8
Views
4K
Replies
4
Views
278
Replies
4
Views
376
Replies
0
Views
398
Replies
4
Views
393

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.

### Which adblocker are you using?

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

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