Error in my VBA code for putting sum total of column A in cell A1

kettler

New Member
Joined
Aug 9, 2011
Messages
20
I'm trying to learn some VBA and in my testing/learning, I'm trying to put the sum total of col A in cell A1 and with this below code Im able to get my answer.

Range("A1") = WorksheetFunction.Sum(Range("A:A"))

However, Im trying to tweak the code such that the data should only check from cell A2 to the last data cell in col A (dynamic - as my no of rows can change) even if there are blank/emply cells or null value cells in between. And using this code, I'm getting an error (see bottom). Any recommendations? Btw this is the only code in my subroutine.

Range("A1") = WorksheetFunction.Sum(Range("A2:A" & lastrow))

Thank you for your suggestions.

1617975272172.png



1617975394125.png
 

Attachments

  • 1617974985033.png
    1617974985033.png
    2.3 KB · Views: 4
  • 1617975383201.png
    1617975383201.png
    4 KB · Views: 5

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Without any useless variable as a beginner starter :​
VBA Code:
    [A1].Value2 = Application.Sum(Range("A2", Cells(Rows.Count, 1).End(xlUp)))
 
Upvote 0
Solution
@Fluff - trying to get to the last data cell in the column.

@Marc L - yes that worked very well. Thank you for your input. I learnt something new in VBA today. :)(y)
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

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