+ Reply to Thread
Results 1 to 16 of 16

VBA - COUNTIF "not equal" to different criteria

  1. #1
    Registered User
    Join Date
    05-23-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    84

    VBA - COUNTIF "not equal" to different criteria

    Hi all,

    I'm looking for some help on creating a not equal to COUNTIF or SUMPRODUCT that will go through a specific column (G) and will check all the cells for each abbreviation for the 50 states. If a cell does not equal one of the 50 abbreviations, it will be counted.

    If I need to explain more, please let me know. Any help would be great.

    Thanks.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA - COUNTIF "not equal" to different criteria

    Try this custom function:

    Please Login or Register  to view this content.
    To use just go to whichever cell you want the count reported and insert =CountNonStates(G1:GX) where X is the last row. Or you could just do G:G if you want the whole column

    Note: You will need to take the time to add all 50 states. I added 4 for you.

  3. #3
    Registered User
    Join Date
    05-23-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: VBA - COUNTIF "not equal" to different criteria

    Hmm. Didn't seem to work. Maybe I did something wrong, but it didn't end up counting the number of non-states.

    Anybody else have any suggestions?

  4. #4
    Registered User
    Join Date
    05-23-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: VBA - COUNTIF "not equal" to different criteria

    Just bumping.

  5. #5
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA - COUNTIF "not equal" to different criteria

    Hmm. It works just fine for me. What value are you getting back? Did you enable macros in the workbook?

  6. #6
    Registered User
    Join Date
    05-23-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: VBA - COUNTIF "not equal" to different criteria

    It's just giving me an error. I'll give you the code I have entered in my VBA and I'll try to explain it to you. Basically, I'm using this function / formula to count all the unknown / foreign states listed within a large census folder (e.g. anything not recognized as a state in the US).

    Please Login or Register  to view this content.
    I want the count to be placed in Cell G57, in the worksheet Initial Tester, under the Initial Demographics tab. Using the formula CountNonStates, in the worksheet Census Tester, under the Census tab, checking data from column G.

    Thanks.

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: VBA - COUNTIF "not equal" to different criteria

    A formula like
    =SUMPRODUCT(--ISNUMBER(SEARCH(C1:C100&",","AZ,CA,MN,"))*(C1:C100<>"")) should do this.

    Note that the constant string "AZ,CA,MN," ends in a comma.
    In practice, it would be easier to have a named string constant of all the state abreviations.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  8. #8
    Registered User
    Join Date
    05-23-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: VBA - COUNTIF "not equal" to different criteria

    Quote Originally Posted by mikerickson View Post
    A formula like
    =SUMPRODUCT(--ISNUMBER(SEARCH(C1:C100&",","AZ,CA,MN,"))*(C1:C100<>"")) should do this.

    Note that the constant string "AZ,CA,MN," ends in a comma.
    In practice, it would be easier to have a named string constant of all the state abreviations.
    Hi Mike, I tried your formula and as I was entering it in VB it kept giving me a "Expected end error" at the beginning where you have (C1:C100&",",").

  9. #9
    Registered User
    Join Date
    05-23-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: VBA - COUNTIF "not equal" to different criteria

    EDIT: double-post. oops.

  10. #10
    Registered User
    Join Date
    05-23-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: VBA - COUNTIF "not equal" to different criteria

    Anybody else have any ideas?

  11. #11
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: VBA - COUNTIF "not equal" to different criteria

    That is a worksheet formula. In VBA, try
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    05-23-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: VBA - COUNTIF "not equal" to different criteria

    Hi Mike, I'll give it a go here in a little bit and get back to you.

    Thanks.

  13. #13
    Registered User
    Join Date
    05-23-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: VBA - COUNTIF "not equal" to different criteria

    Quote Originally Posted by mikerickson View Post
    That is a worksheet formula. In VBA, try
    Please Login or Register  to view this content.
    Mike, I put this into my code and it ran through all the way, but instead of counting all the instances that weren't equal to the state abbreviations, it counted all that WERE a state abbreviation.
    Last edited by crayhons; 06-12-2013 at 11:41 AM.

  14. #14
    Registered User
    Join Date
    05-23-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: VBA - COUNTIF "not equal" to different criteria

    Anybody else know how I can change the code to make it count the non-instances of the state abbreviations?

    Thanks.

  15. #15
    Registered User
    Join Date
    05-23-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: VBA - COUNTIF "not equal" to different criteria

    Still without a solution if anyone else cares to take a crack at it.

    Thanks

  16. #16
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: VBA - COUNTIF "not equal" to different criteria

    If the formula counted the "has abbreviation" why not subtract that number from the total number of rows in the range?

+ 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