How do I sum a range with last rows

Lauren123

New Member
Joined
Mar 16, 2018
Messages
28
I am not sure how to do this on VBA, I want to sum a range that will start on P7, and also where will it show me the answer?

Sub suma()
Dim rng As Range
Dim answer As Long


LastRows = Sheets("Draft").Range("P" & Rows.Count).End(xlUp).Row


Set rng = AD.Worksheets("Draft").Range("P7" & LastRows)


answer = Application.WorksheetFunction.Sum(rng)


End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
"P7:P" & LastRows

will result in P7:P100, for example

assuming the same value for lastrow you were basically doing... P7100
 
Last edited:
Upvote 0
Maybe something like this...

Code:
Sub suma()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim rng As Range
    Dim answer As Long
    Dim LastRow
    
    Set wb = ActiveWorkbook 'change accordingly
    Set ws = wb.Sheets("Draft")
    
    With ws
        LastRow = .Range("P" & .Rows.Count).End(xlUp).Row
        Set rng = .Range("P7:P" & LastRow)
    End With
    
    answer = Application.WorksheetFunction.Sum(rng)
    
    MsgBox answer, vbInformation


End Sub

To refer to a specific workbook instead of the active workbook...

Code:
Set wb = Workbooks("Book2.xlsm")

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,834
Members
449,471
Latest member
lachbee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top