+ Reply to Thread
Results 1 to 10 of 10

Inserting countifs formula using vba.....having problem with date

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Inserting countifs formula using vba.....having problem with date

    I am trying to put the following formula into an excel sheet via vba.........

    Please Login or Register  to view this content.
    but its giving me "0"

    if i change the formula to this.........it works

    Please Login or Register  to view this content.

    problem i am having is in my vba code i have a variable

    current_month = "9/1/11"

    But no matter what i try i can seem to get the date into the right format for this formula to be inserted correctly........any ideas?
    Last edited by welchs101; 10-26-2011 at 07:18 PM.

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Inserting countifs formula using vba.....having problem with date

    Have you looked into the DATE function and the DATEVALUE functions? Also, can you describe how the current_month="9/1/2011" variable is interfering? Can you post your code and sample workbook?
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Inserting countifs formula using vba.....having problem with date

    Please attach the sample workbook with the code so we can help you out easily.

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Inserting countifs formula using vba.....having problem with date

    i am posting an example.

    see cell C1........its here that i am trying to get the same results as in C4
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Inserting countifs formula using vba.....having problem with date

    Please Login or Register  to view this content.
    The problem is countifs is expecting criteria to be quoted, and your variable was dimensioned as a date data type. Removing the type declaration allowed me to surround the date with quotes as a string.

  6. #6
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Inserting countifs formula using vba.....having problem with date

    what about this though




    Please Login or Register  to view this content.

  7. #7
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Inserting countifs formula using vba.....having problem with date

    Had a devil of a time getting it to accept the quotes, so I used Chr(34) which is the ASCII for the quotation.
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Inserting countifs formula using vba.....having problem with date

    thanks!!!!!!!!!!! i could not figure it out at all........getting those quotes in there was tough but you did it.......never would have thought of using what you did ..........thanks.

  9. #9
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Inserting countifs formula using vba.....having problem with date

    Glad to help! Please mark the thread closed, and if you're so inclined, you can add to my reputation.

  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,447

    Re: Inserting countifs formula using vba.....having problem with date

    For future reference, the best way to get a formula in VBA is to start in Excel

    Put the formula you want in the cell where you want it. Check it returns what you expect.
    Start the macro recorder.
    Press F2 to go into Edit mode
    Press Enter to (re-commit) the formula
    Stop the macro recorder.

    You'll end up with something like:

    Please Login or Register  to view this content.

    Regards
    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


+ 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