macro calculate absolute difference between two columns

beelzebubble

Board Regular
Joined
Jun 11, 2009
Messages
68
Hi,

I'm using excel 2003. Using the macro recorder, I was able to produce vba code to calculate the absolute difference between two values for 15 rows of data. That is, 15 data points in column A and 15 data points in column B. The absolute difference values are places in column C.

Code:
Sub test()
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "=ABS(RC[-2]-RC[-1])"
    Range("C1").Select
    Selection.AutoFill Destination:=Range("C1:C15")
    Range("C1:C15").Select
End Sub

I am trying to modify the above code to make it more flexible, but without success. That is, I would like the code to work on data sets that may have different numbers of rows (i.e., without having to manually change the number of rows). Say, one data set might have 37 rows and another might have 337. Secondly, I would like the code to place the absolute difference scores in the next available column in the excel sheet, rather than specifying the column.

Thanks for your help.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Code:
Sub Test1()
Application.ScreenUpdating = False
Dim LastRow As Long, NextColumn As Integer
LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
NextColumn = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
Range(Cells(1, NextColumn), Cells(LastRow, NextColumn)).FormulaR1C1 = "=ABS(RC1-RC2)"
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Code:
Sub test()
    With Range("A1", Range("a" & Rows.Count).End(xlUp))
        .Offset(, Cells(1, Columns.Count).End(xlToLeft).Column).Formula = _
        "=abs(a1-b1)"
    End With
End Sub
?
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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