+ Reply to Thread
Results 1 to 7 of 7

Countif in VBA returns wrong result.

  1. #1
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Countif in VBA returns wrong result.

    I have a list of date in A3:A13 and 1 date in C1
    All dates are formatted as dd/mm/yyy
    I try to countif as follow:
    =COUNTIF(A3:A13,"<="&C1)
    it is OK
    but try with VBA:
    Please Login or Register  to view this content.
    it returns wrong result
    See attachment.
    Anybody can help?
    Attached Files Attached Files
    Quang PT

  2. #2
    Valued Forum Contributor
    Join Date
    10-05-2014
    Location
    Regina, SK Canada
    MS-Off Ver
    Excel for Mac 11
    Posts
    330

    Re: Countif in VBA returns wrong result.

    it appears that the wrong variable is being used (ngay) as your function's first parameter. Possibly a typo.

    This routine worked for me.

    Please Login or Register  to view this content.
    If this has been helpful, please click on the star at the left.

  3. #3
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Countif in VBA returns wrong result.

    Yes, it is typo. At the first time I used "ngay"(date in my language) as variable.
    Anyway, it still does not work with your code.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Countif in VBA returns wrong result.

    Hello
    The wrong variable wasn't the issue for me. When I corrected it I still got 11 with the macro. With me in the UK it's always that VBA sees the dates as US and miscalculates. I convert the date to Long to avoid this. This works for me:

    Please Login or Register  to view this content.
    DBY

  5. #5
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Countif in VBA returns wrong result.

    Thanks DBY, it works now.
    it is first time I hear about this and it seem strange to me.
    Anyway, it must be OK if dates are formatted as yyyy/mm/dd.
    I tried but it is still wrong!!

  6. #6
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Countif in VBA returns wrong result.

    Yes it frustrates me too. Formatting never seems to help. I tried a number of solutions but converting to long seems to work in cases like this. I'm not clear as to the logic that's at work here!

  7. #7
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Countif in VBA returns wrong result.

    That's because you use regional setting that use dd/mm/yyyy syntax (for Vietnam, I guess ?), but mostly VBA commands use English (United States) regional setting (mm/dd/yyyy).

    n = WorksheetFunction.CountIf(adate, ">=" & Range("C1"))
    With C1 = 03/01/2016, this line is treated as :
    n = WorksheetFunction.CountIf(adate, ">=" & 1 March 2016)
    rather than :
    n = WorksheetFunction.CountIf(adate, ">=" & 3 Jan 2016)

    For experiment, (close Excel first) try changing your windows regional setting (through control panel) to "English (United States)" and then run the sub, you will get correct answer.

    The best way to deal with date is like DBY suggestion, using clng(range()) or cdbl(range()) or range().value2
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

+ 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. [SOLVED] Countif returns faulty result
    By boboivan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-23-2015, 12:01 PM
  2. [SOLVED] formula returns wrong result
    By zplugger in forum Excel General
    Replies: 3
    Last Post: 03-12-2014, 01:17 PM
  3. Countif with 3 criteria and 3 exclusions returns wrong count
    By Kelly Linton-Selkirk in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-20-2014, 04:30 PM
  4. [SOLVED] Formula returns wrong result after midnight
    By ujpest in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 05-02-2013, 11:29 AM
  5. isblank & vlookup formula returns #n/a result and incorrect result
    By helpmeplease333 in forum Excel General
    Replies: 5
    Last Post: 05-06-2012, 11:41 PM
  6. [SOLVED] vlookup based on random result returns incorrect result
    By rickat in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-05-2005, 09:20 AM
  7. SUM returns wrong result
    By MarkN in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-19-2005, 11:05 PM

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