+ Reply to Thread
Results 1 to 27 of 27

Countif in VBA with Multiple Conditions

  1. #1
    Registered User
    Join Date
    12-08-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    53

    Countif in VBA with Multiple Conditions

    Hi,

    I am trying to create a VBA Countif function with an OR condition inside if one of the following values is found. For instance, in this code:

    Please Login or Register  to view this content.
    Here, the numerator would contain the count of cells with colors red or blue in the colorColumn, for the i index in the BrandArray in the brandColumn, with a purpose of 1 within the purposeColumn, with the material codes of 1,2,3,7,8,9, or 10 in the materialColumn. But I must be doing something wrong because the code only checks for 1 in the materialColumn, not the full list of numbers (the rest of the code works as normal).

    Do I have the syntax wrong for how to perform this?

    Thank you.
    Last edited by Dord25; 01-09-2024 at 05:56 PM.

  2. #2
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Countif in VBA with Multiple Conditions

    The syntax looks fine to me.
    Rory

  3. #3
    Registered User
    Join Date
    12-08-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    53

    Re: Countif in VBA with Multiple Conditions

    Thank you. Is there any other way to write Array(1, 2, 3, 7, 8, 9, 10) to check if this is the problem, if this is supposed to be the right way to count multiple numbers here?

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: Countif in VBA with Multiple Conditions

    Are red and blue words in a column on the sheet or variables set to some number in your code or cell background colors?
    Ben Van Johnson

  5. #5
    Registered User
    Join Date
    12-08-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    53

    Re: Countif in VBA with Multiple Conditions

    Thank you. Yes, red and blue are set to some number that is hard-coded earlier in the code. The only part that appears to be not counting is the one with the array.

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: Countif in VBA with Multiple Conditions

    When I did a small test on: A 0 0 7 0 7 9 8 1 0

    =COUNTIF(B2:I2,{7,9})
    the formula only returned a count of the occurrences of first value in the array (i.e., 2)
    =COUNTIF(B2:I2,{9,7}) --->1

    sample file, maybe?
    Last edited by protonLeah; 01-09-2024 at 07:08 PM.

  7. #7
    Registered User
    Join Date
    12-08-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    53

    Re: Countif in VBA with Multiple Conditions

    Quote Originally Posted by protonLeah View Post
    When I did a small test on: A 0 0 7 0 7 9 8 1 0

    =COUNTIF(B2:I2,{7,9})
    the formula only returned a count of the occurrences of first value in the array (i.e., 2)
    =COUNTIF(B2:I2,{9,7}) --->1

    sample file, maybe?
    Thank you. Wouldn't that mean that Countif doesn't work this way, if your test doesn't work either?

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Countif in VBA with Multiple Conditions

    If you want the count of 7 and 9 combined, you'd need this:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  9. #9
    Registered User
    Join Date
    12-08-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    53

    Re: Countif in VBA with Multiple Conditions

    Quote Originally Posted by TMS View Post
    If you want the count of 7 and 9 combined, you'd need this:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Thank you. I had Sum wrapped around my original formula above but it didn't make a difference.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Countif in VBA with Multiple Conditions

    You're welcome. Thanks for the rep.

  11. #11
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Countif in VBA with Multiple Conditions

    Quote Originally Posted by Dord25 View Post
    Thank you. Is there any other way to write Array(1, 2, 3, 7, 8, 9, 10) to check if this is the problem, if this is supposed to be the right way to count multiple numbers here?
    Try changing the order of the numbers in the array to see if it changes the result. If it doesn’t, then that is not the issue.

  12. #12
    Registered User
    Join Date
    12-08-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    53

    Re: Countif in VBA with Multiple Conditions

    Quote Originally Posted by rorya View Post
    Try changing the order of the numbers in the array to see if it changes the result. If it doesn’t, then that is not the issue.
    Thank you for the suggestion. Yes, changing the first number of the array changes what is counted. If it reads Array(1, 2, 3, 7, 8, 9, 10) it only counts the 1s in the file; if Array(2, 3, 7, 8, 9, 10), only the 2s, Array(4, 10) only the 4s (that match all the other conditions).

    This is only a guess, but could the issue have anything to do with the type of array that these are? In the file, these numbers are simply numbers, not text as numbers.

  13. #13
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Countif in VBA with Multiple Conditions

    No, if that were the case it just wouldn't work for any of them. I'm assuming that red and brandarray(i) are not themselves arrays? ( I Know brandarray is an array, but am assuming the items within it are not also arrays)

    Can you post a workbook with the code where it doesn't work for you? (only the minimum code required to demonstrate the problem)

  14. #14
    Registered User
    Join Date
    12-08-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    53

    Re: Countif in VBA with Multiple Conditions

    Thank you. Yes, red, blue, and i in brandarray(i) just represent hard coded numbers to match within the worksheet. They're not arrays.

    I can post a workbook but it will take a lot of time to condense the code to the smallest data set possible. Are there any other ideas first to look into? Maybe there is another function I could use that could do the same as Countif in a different way?

  15. #15
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Countif in VBA with Multiple Conditions

    I've attached a minimal sample file. Can you just verify that running the foo macro produces a message with the same number as the formula on the sheet?
    Attached Files Attached Files

  16. #16
    Registered User
    Join Date
    12-08-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    53

    Re: Countif in VBA with Multiple Conditions

    Thanks for the worksheet. Yes, I get 3 on the sheet. But when I add another condition like so:

    Please Login or Register  to view this content.
    I get 1, when we should get 2.

    counitfs test update.xlsm

  17. #17
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Countif in VBA with Multiple Conditions

    Yes, that is true but it is not the case in your code (that is why I asked if either of the other conditions were arrays).

    If you need two arrays, one has to be a vertical array and the other horizontal

  18. #18
    Registered User
    Join Date
    12-08-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    53

    Re: Countif in VBA with Multiple Conditions

    Quote Originally Posted by rorya View Post
    Yes, that is true but it is not the case in your code (that is why I asked if either of the other conditions were arrays).
    No, they are not, but BrandArray is an array as well. It is variant type and contains codes like 1453, 1698, etc. that are matched on.

    The column I added to the file has the same kind of format of the column in the code above that is not working (with Array(1, 2, 3, 7, 8, 9, 10))

    Quote Originally Posted by rorya View Post
    If you need two arrays, one has to be a vertical array and the other horizontal
    Sorry, I am confused. Is there some documentation on what kind of rule this is? How would it work for the foo macro? Thank you.
    Last edited by Dord25; 01-11-2024 at 09:24 PM.

  19. #19
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Countif in VBA with Multiple Conditions

    Quote Originally Posted by Dord25 View Post
    No, they are not, but BrandArray is an array as well.
    Yes, I know, but you are not passing the whole array at once as a criterion, you are passing individual elements one at a time using BrandArray(i). Unless the individual elements of your BrandArray array are also themselves arrays, that is not the issue (your code is posted completely out of context so we can't tell).

    Sorry, I am confused. Is there some documentation on what kind of rule this is? How would it work for the foo macro? Thank you.
    No, because it's not a normal use of countifs. If you use two arrays with the same orientation, the criteria are taken in pairs but what you want is all possible combinations (a Cartesian product). So for example, if you pass the arrays {1,2} and {3,4} as criteria, you would get rows matching 1 and 3, and rows matching 2 and 4 only. You would need the equivalent of {1,2} and {3;4} in formulaic terms.

  20. #20
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Countif in VBA with Multiple Conditions

    Quote Originally Posted by Dord25 View Post
    Thanks for the worksheet. Yes, I get 3 on the sheet. But when I add another condition like so:

    Please Login or Register  to view this content.
    I get 1, when we should get 2.

    Attachment 855466
    One array needs to be transposed - for example:

    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    12-08-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    53

    Re: Countif in VBA with Multiple Conditions

    Thanks for the arcane tip. I did not know about this rule. Is this because of how Countif operates?

    Here is the full loop:

    Please Login or Register  to view this content.
    So in numerator2 should we have:

    Please Login or Register  to view this content.
    Last edited by Dord25; 01-12-2024 at 12:46 PM.

  22. #22
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Countif in VBA with Multiple Conditions

    Yes. It would have saved a lot of time if you'd posted the relevant code to start with.

  23. #23
    Registered User
    Join Date
    12-08-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    53

    Re: Countif in VBA with Multiple Conditions

    Sorry about that--I thought it would be too much to read.

    What do I do if I want to add a third or fourth Array to these COUNTIF conditions? Is that possible?

  24. #24
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Countif in VBA with Multiple Conditions

    No, it isn't. You'd need a different approach.

  25. #25
    Registered User
    Join Date
    12-08-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    53

    Re: Countif in VBA with Multiple Conditions

    What could that be?

  26. #26
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Countif in VBA with Multiple Conditions

    It would depend on the context. It might be easier to loop through arrays, or use ADO, or filters, or something like a SUMPRODUCT formula inside Evaluate.

  27. #27
    Registered User
    Join Date
    12-08-2017
    Location
    United States
    MS-Off Ver
    2016
    Posts
    53

    Re: Countif in VBA with Multiple Conditions

    SUMPRODUCT inside Evaluate probably would have been more elegant, even here. But what I have gets the job done. Thanks again!

+ 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. Replies: 5
    Last Post: 03-30-2020, 08:47 PM
  2. Using multiple conditions with countif
    By roberco30 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-23-2019, 01:06 PM
  3. CountIF with multiple conditions one of which needs an OR.
    By Paul Clark in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-14-2014, 10:45 AM
  4. COUNTIF and multiple conditions
    By Sami in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-28-2005, 05:05 AM
  5. Multiple conditions on a countif
    By Mr Mike in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 10:05 AM
  6. [SOLVED] Multiple conditions on a countif
    By Mr Mike in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 AM
  7. [SOLVED] Multiple conditions on a countif
    By Mr Mike in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 11:05 PM

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