+ Reply to Thread
Results 1 to 10 of 10

Macro to Sum ALL the rows in a column - any number of rows

  1. #1
    Registered User
    Join Date
    04-17-2013
    Location
    Vallejo, CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Macro to Sum ALL the rows in a column - any number of rows

    I need a macro (at top of the column), to SUM all of the rows below.

    --------------------------------------------------------------------------------------------

    My keystrokes are:

    Please Login or Register  to view this content.
    --------------------------------------------------------------------------------------------

    Excel 2010 converts that to:

    Please Login or Register  to view this content.
    --------------------------------------------------------------------------------------------

    How do I get VBA to sum ALL rows and not just (A2:A4478) ?
    Last edited by RM-Vallejo; 04-20-2013 at 05:08 AM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Macro to Sum ALL the rows in a column - any number of rows

    Try this...
    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    08-16-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2019
    Posts
    209

    Re: Macro to Sum ALL the rows in a column - any number of rows

    try this.

    Sub Test()

    range("A1").value = worksheetfunction.sum(range("A:A"))

    end sub

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Macro to Sum ALL the rows in a column - any number of rows

    Quote Originally Posted by lexusap View Post
    try this.

    Sub Test()

    range("A1").value = worksheetfunction.sum(range("A:A"))

    end sub
    lexusap, please use code tags when supplying VBA code, BTW, you can't use A1 in the sum formula, you would get a circular reference.

  5. #5
    Registered User
    Join Date
    04-17-2013
    Location
    Vallejo, CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Macro to Sum ALL the rows in a column - any number of rows

    Quote Originally Posted by davesexcel View Post
    Try this...
    Please Login or Register  to view this content.
    Works the first time I run it, but when more rows are added and I run it again, it sums the first number of rows 4478 and not the new number number of rows 4491.

  6. #6
    Registered User
    Join Date
    04-17-2013
    Location
    Vallejo, CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Macro to Sum ALL the rows in a column - any number of rows

    Quote Originally Posted by lexusap View Post
    try this.

    Sub Test()

    range("A1").value = worksheetfunction.sum(range("A:A"))

    end sub
    This one works everytime I run it, big improvement, but it just plugs in the VALUE and is not a formula, so it doesn't change when rows are deleted.

  7. #7
    Forum Contributor
    Join Date
    08-16-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2019
    Posts
    209

    Re: Macro to Sum ALL the rows in a column - any number of rows

    how about this? Assuming all your data is in column A

    Please Login or Register  to view this content.
    Result will change when you add or delete data

  8. #8
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Macro to Sum ALL the rows in a column - any number of rows

    Why not use an actual formula, instead of VBA that you originally asked for?
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Place the formula in A1 and the sum will change automatically.

    65536 is used for worksheets that only have 65536 rows, so if your workbook is in compatibility mode it will work.

    Why would you want a VBA code for this in the first place, if you don't want to use it?
    Last edited by davesexcel; 04-19-2013 at 09:47 PM.

  9. #9
    Registered User
    Join Date
    04-17-2013
    Location
    Vallejo, CA, USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Macro to Sum ALL the rows in a column - any number of rows

    Quote Originally Posted by davesexcel View Post
    Why not use an actual formula, instead of VBA that you originally asked for?
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Place the formula in A1 and the sum will change automatically.

    65536 is used for worksheets that only have 65536 rows, so if your workbook is in compatibility mode it will work.
    SOLVED. This works really well, even if the data has blank rows or rows with text in them. Using the huge expotiential number is very clever, it is almost like your taking advantage of a known bug there can't be a number larger than that, so it goes to the next highest it can find.

    Why would you want a VBA code for this in the first place, if you don't want to use it?
    I'm working on a project to automate a time consuming manual process, where a data file gets emailed to us every month and then we prep the data (format, sort, sum, count, pivot table, paste special values) the same way every time and finally output it as a .txt file which gets uploaded into our accounting software as a journal entry.

    I tried using the macro recorder but it doesn't type the keystrokes EXACTLY as I type them, it converts them to VBA code, which uses (A2:A478) instead of {CTRK}+{SHIFT}+{DOWN} and my macro only works if the number of rows of data doesn't change. So I'm trying to figure out a work around, like this one for =SUM() everytime I run into the (A2:A478) reference problem in my recorded VBA Code.

    PS - And a big thanks for this, I'm originally from Canada (west coast), just temporarily working in the US.

  10. #10
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Macro to Sum ALL the rows in a column - any number of rows

    With the formula in A1.
    Record Macro
    Select A1
    Select the Formula Bar and hit Enter
    Stop recording

    This is what your recorded macro looks like
    Please Login or Register  to view this content.
    Now you can clean it up
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1