Search code examples
excelvbacell

subtract between last cell from a column and last cell from another column, and put the result in a specific cell


I need some vba code to subtract between last cell from column B and last cell from column D and put the result in cell N1. The position of last cell, in both columns may variate.

Thx


Solution

  • This code does the trick for you. It takes the last cell of both column B and D and it subtracts their value on the N1 cell:

    Sub MySub()
        Dim lastBRow As Long
        Dim lastDRow As Long
    
        lastBRow = Range("B" & Rows.count).End(xlUp).Row
        lastDRow = Range("D" & Rows.count).End(xlUp).Row
        Range("N1").Value = Range("B" & lastBRow).Value - Range("D" & lastDRow).Value
    End Sub