+ Reply to Thread
Results 1 to 28 of 28

VBA: Total sum range

  1. #1
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    VBA: Total sum range

    Hi,

    have the code below can I change it to:

    1. Add total of printed
    2. to print only due dates (select due date range) instead


    Please Login or Register  to view this content.
    thanks.

  2. #2
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: VBA: Total sum range

    Dude. This has been viewed over 400 times, but no answers. We need to see a bit more of your workbook and code to be able to offer any help.

    If you have a look at the yellow band at the top of the page it'll explain how to post a (cut-down, desensitised) version of your workbook so we can look into helping you.

    Tim
    Never stop learning!
    <--- please consider *-ing !

  3. #3
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: VBA: Total sum range

    Attached sample file.

    thank you
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: VBA: Total sum range

    Any help, please?

  5. #5
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: VBA: Total sum range

    If you make the minor changes (below) the sub will just print out what's visible. That means you can use the Status and Past Due etc buttons to do the filtering, and just print-preview the table holding those data.
    Please Login or Register  to view this content.
    Was that the intention? I'm not sure what you mean by "Add total of printed". You could display the "totals" row in the table and include that in your print, but unfortunately, non-contiguous ranges always print on separate pages, so it won't make for a neat and tidy print-out.

    Tim
    Last edited by harrisonland; 04-13-2020 at 10:39 PM.

  6. #6
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: VBA: Total sum range

    Hi,

    I tried but only print first row and there's more than one row visible.

    and for the total to add total amount of visible amounts.

    thanks,

  7. #7
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: VBA: Total sum range

    Have you "PageDown'ed"? Non-contiguous ranges show on separate pages - and it seemed to work when I tried it before posting.

    On the attached, hit "Past Due Age" 1-30 days, then hit "Print Dues". My result is 2 pages (was three, but I tweaked it) Page 1 is the data range, Page 2 the totals row.

    Tim
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: VBA: Total sum range

    Hi,

    I tried in workbook works but in mine give me run-time eror 5Attachment 672551 when press debug highlight this line:

    Please Login or Register  to view this content.
    code below:

    Please Login or Register  to view this content.
    Thanks

  9. #9
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: VBA: Total sum range

    Hi,

    think the error is because of
    Please Login or Register  to view this content.
    give me error label no defined and if I remove that line and add
    Please Login or Register  to view this content.
    then prints preview fine but when close get error 91.

    sorry to bother but I am new vba.

    thanks

  10. #10
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: VBA: Total sum range

    Duh... sorry, my bad. Had another look and improved. Should be this:
    Please Login or Register  to view this content.
    This also fixes the issue with it printing on multiple sheets (I've no idea why I didn't realise what was happening before!)

    I'll re-upload the attachment here.

    Tim
    Attached Files Attached Files

  11. #11
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: VBA: Total sum range

    NB forgot to say, you could make the print visually larger if you don't need all the columns in the table to be printed. E.g. instead of just generating the print preview, you could do this instead:
    Please Login or Register  to view this content.
    Only you can decide what you need, but hiding columns while forcing the print into one sheet wide will certainly make the writing clearer.

    Tim

  12. #12
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: VBA: Total sum range

    thank you so much worked perfectly.

    is it possible to add subtotal function in the code instead, and no in the spreadsheet because need to add more records and and will not let me cause of subtotal formula.

    Thanks again.

  13. #13
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: VBA: Total sum range

    Calculating the subtotal separately would be a pain. You'd have to copy the data to be printed to a separate, temporary worksheet, insert the calculations, set columns widths and print range, print that sheet and then delete it. You could, of course, set up a print template for that purpose and leave it sat there. If you create totals using "=sum(Ax:Ay)" it will count add all values, not just those visible.

    Personally, I'd keep it simple and use one of these methods to insert new rows into the table and let the worksheet do everything else itself:
    • Hit [TAB] from the last cell in the data range of the table (J37 in the sample you provided)
    • Right-click any cell in the table and hit "Insert" -> Insert table row. It'll be sorted later anyway.
    • Grab the little green corner of the table (bottom right of the Totals row in column J) and drag it down however many new rows you need.

    Tim

  14. #14
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: VBA: Total sum range

    Hi Tim,

    I tried your suggestion about totals and entered formula =SUBTOTAL(109,[INVOICE AMOUNT]) but I am getting this error (attaching picture)
    any suggestion?

    thanks
    Attached Images Attached Images

  15. #15
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: VBA: Total sum range

    Sorry, I've absolutely no idea what that means because I have absolutely no context for that formula. Anyway...

    I've created a print template in the attached file and linked the print button to it. Obviously the print template could then be hidden (or xlVeryHidden to hinder meddling) after you've set it up the way you like it.

    The print range is simply the table, but you'll see in the VBA code you can enter headers and/or footers for the print too. I suppose you could adjust the print area to include row(s) above the table and put a title or whatever there, too. Study the code so you know what's going on and how to adapt it!


    Tim
    Attached Files Attached Files

  16. #16
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: VBA: Total sum range

    thank you, Very much.

    worked excellent!

    the only las question i have is how do I hide the template by using
    Please Login or Register  to view this content.
    have no idea no that good in vba.

    thanks.

  17. #17
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: VBA: Total sum range

    You have to do it either manually in the VBA environment, or in code.

    Open up the VBE and hit F4 (I think) to bring up the Properties page if it's not already visible. Select the worksheet, and change the Visibility to xlVeryHidden. Then it can't be unhidden without accessing the VBE.

    Tim
    Last edited by harrisonland; 04-20-2020 at 01:43 PM.

  18. #18
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: VBA: Total sum range

    Tried give an error.

    is there any way in the print code to unhide the sheet when click print and hide it back when previewing it.

    thanks.

  19. #19
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: VBA: Total sum range

    I'll have a look when I get home. It'll be easier than faffing about on the phone.

    Tim

  20. #20
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: VBA: Total sum range

    thank you.

  21. #21
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: VBA: Total sum range

    Right. It won't generate the print preview while the print template worksheet is not visible, so we'll just unhide it to do the print preview, then hide it again afterwards.

    See the attached.

    Tim
    Attached Files Attached Files

  22. #22
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: VBA: Total sum range

    Hi Tim.

    worked good the only problem prints ok first time, once you filter and filter another range will ask if it want to overwrite and said yes the total formula is gone.

    Thanks.

  23. #23
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: VBA: Total sum range

    Hi again. Sorry, but I wasn't able to replicate that behaviour; filtering, and re-filtering seemed to work OK, printing completed as expected and the print template was cleared of data ready for the next print.
    Please Login or Register  to view this content.
    The above clears the data from the print template and resizes the table, but it doesn't touch the totals row at the bottom.

    If you're referring to the totals row on the "Invoices" worksheet, I had removed it as I thought you didn't want it to show as this would make it easier to add extra data at the bottom. You can make it visible again by right-clicking anywhere in the table, selecting "Table" then "Totals Row".

    Tim

  24. #24
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: VBA: Total sum range

    Hi Tim,

    it's weird it worked couple times but once it ask to overwrite the totals are gone,

    no sure if the line you gave me to clear the data I put the in the wrong spot, here is the code:

    Please Login or Register  to view this content.

  25. #25
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: VBA: Total sum range

    Nah, those lines are already in the code so you don't need to add them.

    I still can't replicate the problem, even though I've been round and round filtering, generating print, clearing... doing the same without clearing filters etc.etc.etc... (Just to be clear, I'm running the "test()" sub in module 2.)

    I'd suggest you run the macro manually - step by step, using F8. Then you'll be able to see the effect each step is having in the workbook. Remember to make the print template visible to start with and maybe comment out the line that hides it.

    Tim

  26. #26
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: VBA: Total sum range

    Hi Tim,

    I tried F8 step by step and think found out what's the problem was looks now it's working

    What I did enable the this line back because it was comment out and clears all format no the formulas i filtered list few times and still works.

    line that was commented out and enable back:

    Please Login or Register  to view this content.
    Thanks so much for all your time and help, much appreciated.

  27. #27
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: VBA: Total sum range

    Hi there. Sorry, I've been away for a few days. Glad you got it sorted.

    Tim

  28. #28
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,719

    Re: VBA: Total sum range

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Create a formula to fill a range with a preset value to achive a total. Total will vary
    By bajdr47 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-08-2017, 10:47 AM
  2. Replies: 3
    Last Post: 10-15-2015, 01:06 PM
  3. [SOLVED] Match total of a range
    By bmbalamurali in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-06-2015, 05:39 AM
  4. Replies: 11
    Last Post: 07-15-2012, 04:08 PM
  5. Range Total
    By sagar in forum Excel General
    Replies: 6
    Last Post: 11-19-2008, 12:50 AM
  6. [SOLVED] how to get a total to add up a range
    By andyhofer in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-27-2006, 02:50 PM
  7. [SOLVED] Range Total
    By Himansu in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-09-2005, 05:50 PM

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