Search code examples
vbaexcelexcel-2013

Store Cell Value In Variable


I am trying to store the value of a cell in a variable using vba. My code throws an error, what should I change so that the value of the cell is stored in my variable?

Dim total As Long
total = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).End.Value
Debug.Print (total)

The error I get is

Wrong number of arguments or invalid property assigned


Solution

  • Dim total As Long
    Dim wk as worksheet
    
    Set wk = activesheet
    
    total = wk.Range("D" & wk.Cells(wk.Rows.Count, "D").End(xlUp).Row).value
    Debug.Print (total)