dcsimg
Results 1 to 4 of 4

Thread: [RESOLVED] VBA CountIFs - What am I doing wrong

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,136

    Resolved [RESOLVED] VBA CountIFs - What am I doing wrong

    I have a range of cells named rngSet that can contain numbers from 1 to 79. I want to see if the count of the numbers between 1 and 9 is greater than 2 so I did this.

    Code:
    If Application.WorksheetFunction.CountIfs(rngSet, ">=1" And "<=9") > 2 Then...
    but in a rngSet that I know has more than two occurrences of 3, the result of that CountIfs is 0. Why?

    I tried using rngSet.cells but that didn't help.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,125

    Re: VBA CountIFs - What am I doing wrong

    Could you post a sample-workbook?

    EDIT: Think i found it (at least directly inside the Sheet)
    =COUNTIFS(rngSet,">=1",rngSet,"<=9")

    You still can only use 1 criteria per Range, but you just provide the same Range again for the second criteria. COUNTIFS uses internally the "And" between criteria

    Application.WorksheetFunction.CountIfs(Me.Range("rngSet"), ">=1", Me.Range("rngSet"), "<=9") works correctly from VBA-code

    Replace "Me" with a qualified Sheetname if the function is outside of the worksheet
    Last edited by Zvoni; Feb 17th, 2020 at 04:36 AM.
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,767

    Re: VBA CountIFs - What am I doing wrong

    looks like you need to try like
    Code:
    If Application.WorksheetFunction.CountIfs(rngSet, ">=1", rngsest, "<=9") > 2 Then...
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width