Sum with variable in VBA macro

Hello,

I have a small issue with performing a simple addition of a certain number of cells in the same column using a variable.

The number of cells may vary with each execution of the macro.

For example:

I want to sum from cell D2 to cell Dx (where x is my variable) and have the total in cell Dx +1.

At the beginning of my macro, I initialize the variable but after that, I can't seem to reuse that same variable in the sum function that I want to perform.

Thank you for your help...
Configuration: Windows XP Internet Explorer 6.0

10 answers

  1. Hello,
    I'm a bit late, I don't know if this is what you wanted:

    Sub add()
    Dim lastRow As Long
    Dim lastCol As Long

    lastRow = Range("D3").End(xlUp).Row
    lastCol = Range("D2").End(xlToRight).Column

    Cells(lastRow + 1, 4) = Application.Sum(Range(Cells(2, 4), Cells(2, lastCol)))

    End Sub


    a+
    0