Hello, I wrote a function that calculates the absolute difference between two cells, subtracting 70,000 from the total each time. I'd like to do this in Visual Basic for Applications to make it look prettier.

`70000-(IF(D2>0,ABS(D2-C2))+IF(E2>0,ABS(E2-D2))+IF(F2>0,ABS(F2-E2))+IF(G2>0,ABS(G2-F2))+IF(H2>0,ABS(H2-G2))+IF(I2>0,ABS(I2-H2))+IF(J2>0,ABS(J2-I2))+IF(K2>0,ABS(K2-J2))+IF(L2>0,ABS(L2-K2))+IF(M2>0,ABS(M2-L2))+IF(N2>0,ABS(N2-M2))+IF(O2>0,ABS(O2-N2)) )`
Jan 15 in Others 135 views

## 1 answer to this question.

Use a for loop:

```Sub AbsoluteDifference()
' Store variables as double to account for large numbers and decimals
Dim n As Double
Dim sum As Double

n = 70000
sum = 0

For Each i In Range("D2:O2").Cells
If i.Value > 0 Then
sum = sum + Abs(i.Value - i.Offset(0, -1).Value)
End If
Next i
' Save the value to C3
Range("C3").Value = n - sum
End Sub
```

This code produces an identical result to your function.

• 63,160 points

## Mention the difference between Data Driven Testing and Retesting?

Retesting:  It is a process of checking ...READ MORE

+1 vote

## What is the difference between Dark Web and Deep Web?

The content that which you cannot find ...READ MORE

## What is the difference between BASH and DOS?

The major difference between the BASH and ...READ MORE

## Convert Rows to Columns with values in Excel using custom format

1 I having a Excel sheet with 1 ...READ MORE

## Remove formulas from all worksheets in Excel using VBA

Try this : Option Explicit Sub test1() ...READ MORE

## Calculate monthly average from daily data without PivotTable

Assuming you have the months in column D enter ...READ MORE

## Automate compound annual growth rate (CAGR) calculation

The following PowerPivot DAX formulas worked for ...READ MORE