+ Reply to Thread
Results 1 to 7 of 7

VBA Countifs not working with date

  1. #1
    Registered User
    Join Date
    03-21-2014
    Location
    Dublin Ireland
    MS-Off Ver
    Excel 2013
    Posts
    3

    VBA Countifs not working with date

    Hi,

    I have a piece of code that will not work with date. I have other similar code that works with strings, numbers but this one is stumping me ...
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer

    Please Login or Register  to view this content.

    It is supposed to give me a count of the number of rows with a date older than three days ago where the value in another column has "DM". r* variables refer to the ranges.

    A formula in a cell in the spreadsheet works correctly ...

    Any thoughts?
    Last edited by 6StringJazzer; 04-30-2014 at 03:33 PM.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,296

    Re: VBA Countifs not working with date

    Are you defining your ranges properly? Your code, as such, works fine for me. In an blank workbook, run this, which should return a message "6"

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA Countifs not working with date

    Fàilte mcasserl,

    It could be that your dates having time values attached to them. Since all dates are have two parts, the date portion and the time portion, this can effect the outcome of your results.

    For example, today is 30 April 2014 is your "threedaysago" value. If a date you are comparing is 27 April 2014 should be true. Unless that date contains a time value that is not displayed. If the value was really 27 April 2014 10:30 AM, the compare will fail. Your "threedaysago" value starts at midnight because no time value is assigned. Does that make sense?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    03-21-2014
    Location
    Dublin Ireland
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: VBA Countifs not working with date

    Thanks Bernie/Leith,

    I ran the sample code on a different system (using a US date/time/region setting) and it worked perfectly as does my code (when I thought to do that!)

    I'm not sure what differences in the laptop I normally use might be affecting the running of this macro ... is there something in the euro vs US date that could mess things up and how might I workaround that?

    Thanks,
    -Michael

  5. #5
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,296

    Re: VBA Countifs not working with date

    When you said "will not work" what did you mean - that it gave an incorrect value, gives a 0, or it just does not run?

    VBA is US-centric, so it is possible that dates strings with ambiguous month/day combos (5/2/2014 is ambiguous, 5/21/2014 is not) may be interpreted M/D rather than D/M. Are the cell values true dates, or strings?

    Can you keep a working formula on a sheet and just read the value from the cell?

  6. #6
    Registered User
    Join Date
    03-21-2014
    Location
    Dublin Ireland
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: VBA Countifs not working with date

    When I run your code on my laptop it returns a 0 ...

    I can manipulate the dates and use WS formulas normally with no errors which is how I am working around this at the moment.

  7. #7
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,296

    Re: VBA Countifs not working with date

    There should be some dates that are not ambiguous, since we are at 4/30, and the dates should range from 4/15 to 4/30, then ambiguous dates that would not be counted anyway. I have no way to test solutions.... sorry.

+ 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] { } not working in countifs
    By Chetansuri in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-03-2014, 04:46 AM
  2. countifs and date not working right.
    By kztdat in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 11-22-2013, 06:31 PM
  3. [SOLVED] countifs working on extended ranges and different sheets not working
    By etaf in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-18-2013, 02:23 PM
  4. [SOLVED] Countifs code not working
    By newbi004 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-08-2013, 06:09 AM
  5. COUNTIFS not working
    By nosenga in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-13-2011, 04:38 AM

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