You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- Thread starter kjaypal2002
- Start date

The number in the DIFF column is to be subtracted from H to C in such a way that the cell where the number is less than DIFF is zero, and the cell where the number is greater is subtracted from the remainder and the DIFF column is zeroed. There is nothing to do where there is no DIFF. Please see the attached file to know more. Data tables should look like this

Code:

```
Sub ReverseSubtraction()
Dim ws As Worksheet
Dim lastRow As Long, lastCol As Long, i As Long, j As Long
Dim diffCol As Long
' Set the worksheet
Set ws = ThisWorkbook.Sheets("Sheet1") ' Replace "Sheet1" with the actual name of your sheet
' Find the last row and column in the worksheet
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
' Find the column index for the DIFF column
diffCol = Application.Match("DIFF", ws.Rows(1), 0)
' Loop through each row in reverse order
For i = lastRow To 2 Step -1
' Loop through each column from H to C in reverse order
For j = lastCol - 1 To 8 Step -1
' Subtract the value in the DIFF column
If ws.Cells(i, diffCol).Value > 0 Then
ws.Cells(i, j).Value = WorksheetFunction.Max(ws.Cells(i, j).Value - ws.Cells(i, diffCol).Value, 0)
ws.Cells(i, diffCol).Value = IIf(ws.Cells(i, j).Value = 0, 0, ws.Cells(i, diffCol).Value - ws.Cells(i, j).Value)
End If
Next j
Next i
End Sub
```

Make sure to replace "Sheet1" with the actual name of your sheet. This VBA code performs the reverse subtraction as described in your example. It loops through each row and each column in reverse order, adjusting values based on the DIFF column.

- Cross-Posting. Generally, it is considered poor practice to cross post. That is to post the same question on several forums in the hope of getting a response quicker.
- If you do cross-post, please put that in your post.
- Also if you have cross-posted and get an
**Solution**elsewhere, have the courtesy of posting the**Solution**here so other readers can learn from the answer also, as well as stopping people wasting their time on your answered question.

Strange... It must have been deleted for some reason at MrE, and the username is also gone ?Above Your given link isn't valid...

Monty Sir, CHATGPT gave me the right CODE based on the code provided by you, so thank you very much.' Loop through each row in reverse order