+ Reply to Thread
Results 1 to 9 of 9

Getting Totals from multiple sheets VBA

  1. #1
    Forum Contributor
    Join Date
    06-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007; Excel 2010
    Posts
    178

    Getting Totals from multiple sheets VBA

    Hi,

    I am looking for the correct way to find cell value in multiple sheets and return total to offset cell.

    I am only get 1st occurrence of value, but need to get all occurrences of value

    What I am using is not obviously correct but a start.

    Can someone show me the correct way to do this.
    Please Login or Register  to view this content.
    Thank you for your time
    Last edited by Foreverlearning; 04-20-2012 at 08:08 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Getting Totals from multiple sheets VBA

    Have you tried stepping through the code?
    Entia non sunt multiplicanda sine necessitate

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

    Re: Getting Totals from multiple sheets VBA

    You can use the Sumif Function in VBA,
    Since you know what ranges you want in each sheet you can set the ranges in the code.
    This example code will Sum the number 3 in your specified ranges.
    You can edit the code to use your offset designations.
    Please Login or Register  to view this content.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Getting Totals from multiple sheets VBA

    Try it like so.... I've created a couple of arrays to hold your sheetnames and your address strings to shorten up the code a bit.

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 04-20-2012 at 09:43 AM. Reason: fixed the "Next shrng" line of code
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  5. #5
    Forum Contributor
    Join Date
    06-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007; Excel 2010
    Posts
    178

    Re: Getting Totals from multiple sheets VBA

    Thanks guys for your replies....

    @shg yes I stepped thru and it halts on 1st attempt each time except when using PasteAll

    @Dave I will give your code a try even if I use JBeaucaire (essential to learn all ways given)

    @JBeaucaire Mate, yours appeals more as I am still trying to get my head around arrays

    I will post back as soon as I have tried both ways..

    Thanks Guys

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Getting Totals from multiple sheets VBA

    I made a correction and highlighted it in red in post #4.

    I like to think of arrays as just a "list" and each item in the list can be grabbed by "index number". So, the first time through the array loop, we take the first indexed item in BOTH arrays and use them as sheet name and range references. Next time through we take the next pair, etc.


    The correction I made above is because I forgot that arrays are usually 0-based, the first index number is 0, not 1. By using LBound(), I don't have to remember which it is, the macro simply uses the lowest index number for the array whether it is 0 or 1.

  7. #7
    Forum Contributor
    Join Date
    06-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007; Excel 2010
    Posts
    178

    Re: Getting Totals from multiple sheets VBA

    Thank you Jerry for your correction.

    Just so I am on the right track

    The code is putting all mentioned sheets into an array
    and all mentioned ranges into an array

    For each product in range of products the code picks 1st sheet and all ranges added in array and looks for product.
    If product found adds marker and value and looks for next occurrence until all sheets done.
    Then puts total into offset cell.

    Repeats with next product until done.

    So the product is being searched in all sheets ?

    Thanks

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Getting Totals from multiple sheets VBA

    Yes, the product is being searched in each sheet listed in array 1, but only in the range listed in array2 in the same position within the arrays. But it duplicates your original long macro in that way.

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    06-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007; Excel 2010
    Posts
    178

    Re: Getting Totals from multiple sheets VBA

    I Learn more every day though still forever learning

    Thank you Jerry for your time and help..

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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