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

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
"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,223,888
Messages
6,175,215
Members
452,618
Latest member
Tam84

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