+ Reply to Thread
Results 1 to 15 of 15

SumIfs with multiple criteria using VBA

  1. #1
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    SumIfs with multiple criteria using VBA

    Hi all

    I have the following code to sum cost in column J if the following criteria is met:

    1 If the specified GLCode is in column P
    2 If the specified start date (FromDate) is in column L
    3 If the specified end date (ToDate) is also in colmn L
    4 If the specifiedfile name (in this instance MasterFile_00) is in column Z

    My problem is that I need increase the number of criteria 4 to about 20 files to check (MasterFile_12, MasterFile_19, MasterFile_34 and so on).

    How could I amend the code to check occurance of these file names for inculding in the sumifs formula?

    Code :

    Please Login or Register  to view this content.
    Many thanks

  2. #2
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: SumIfs with multiple criteria using VBA

    What would you like to achieve?

    You want to count between differant workbooks (not sheets) on a specified criteria.

    If so, isn't it an option to make a summery workbook with all the values in it (on 1 sheet), and after that use the formula's to get your result?
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  3. #3
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: SumIfs with multiple criteria using VBA

    oeldere

    Thank you for your interest.

    I have a summary workbook which has all the data.

    The formula is trying to sum all money spent by different departments (MasterFile_00, MasterFile_12 etc) for different cost centres (GLCode).

    When I tried the previous code I sent, it gave me the correct answer for money spent by department MasterFile_00. However, I want to check all money spent by departments MasterFile_00 and MasterFile_13 and tried this code which gave me a zero answer:

    Please Login or Register  to view this content.
    How can I sumif with more cost centres. Maybe sumifs is not the right way?

    Thanks

  4. #4
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: SumIfs with multiple criteria using VBA

    If the layout (format) of the data is the same on all the sheets; I would make an summary sheet and after that make an pivot table for your needs.

    You get better help if you add an excel file, without confidentional information.

    Please also add the desired (expected) result.

  5. #5
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: SumIfs with multiple criteria using VBA

    Hi again oeldere

    I attach a sample worksheet which gives the desired result when I use standard formula in cells L37 and L38.

    I summed the sumifs, so maybe this is the way to do in VBA? The problem is that there will be up to 20 sumifs because there are 20 different Cost Centres to deal with! Would there be a simple way to do multiple sumifs rather than write each out 20 times? (...or should this be another thread started?)

    Thanks
    Attached Files Attached Files

  6. #6
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: SumIfs with multiple criteria using VBA

    Hi Newbi004!

    Try This...

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    In case of VBA.. I hope you will able to adjust accordingly with Worksheetfunction, and Array("MasterFile_00","MasterFile_13",...)
    Regards!
    =DEC2HEX(3563)

    If you like someone's answer, click the star to give them a reputation point for that answer...

  7. #7
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: SumIfs with multiple criteria using VBA

    Maybe like this (privot table).

    See the attached file.
    Attached Files Attached Files

  8. #8
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: SumIfs with multiple criteria using VBA

    BTW.. Please find the attached.. SumIfs problem.xlsx

    I have used a Helper Column..
    provide all Desired Cost Center there.. and in Helper column use .. COUNTIF, if any match found..

    Now only use if cost-CenterHelper = 1 to get desired result..

    hope it helps..

  9. #9
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: SumIfs with multiple criteria using VBA

    Hi Debraj Roy...your formula works great in a worksheet, but I can't seem to adapt that to VBA. How can I do that? I know you have used arrays, but I do not know how to put it in VBA. Could you advise?


  10. #10
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: SumIfs with multiple criteria using VBA

    Thank you both for your help

    I used this code to get what I wanted, but will have to repeat it many times with other cost centres to capture all the data:

    Please Login or Register  to view this content.
    Debraj Roy, your formulae has increased my knowledge in using arrays. Thanks

    oeldere, thanks for showing how to create a pivot table using VBA. I will probably use that approach rather than using my code.

    Cheers

  11. #11
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: SumIfs with multiple criteria using VBA

    Hi Newbi004!

    If you are using VBA.. why should you care of WorkSheetFunction...

    Instead try this...
    Please Login or Register  to view this content.

    Hope now you can manage the DatePart..

    Let us know if anything else required..

  12. #12
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: SumIfs with multiple criteria using VBA

    Hi again Debraj

    I changed the code to your suggested one:

    Please Login or Register  to view this content.
    but it is giving a Type Mismatch error. Have I missed some quotes in this?

    Cheers

  13. #13
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: SumIfs with multiple criteria using VBA

    Hi Newbi004..

    Can you please upload you complete code, so that I dont have to add
    With Sheet1,
    Set FromDate
    Set ToDate
    Set GlCode

    etc..

    BTW.. In case of Evaluate..
    No need to add Range( just write L:L and try to replace all (") double quote within Evaluate Function to Double Double Quote ("")

  14. #14
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: SumIfs with multiple criteria using VBA

    Hi Debraj. I am a newbie when it comes to VBA coding, so haven't used Set FromDate etc. I have done it the long winded way! The full code for that part is:

    Please Login or Register  to view this content.
    I amended the code as you suggested, but it doesn't work. I am obviously missing something.

    Thanks

  15. #15
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: SumIfs with multiple criteria using VBA

    Hi Newbi004..

    Try This one...

    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