Get Current Date In VBA

  • I have a calendar in my spreadsheet that returns a date selected to a cell that is active.
    When the user clicks cell D40 in worksheet Surcharges, I use a Worksheet_SelectionChange event to show the calendar. When the user selects a date it returns the date in that cell.
    Problem is I want to limit the user to select a date no more than 90 days from today's date.
    I was having a problem using today() in the vba code.
    When i enter todays date in cell E39 (-today()) and use the code below it works:


    But when I try to use the today() function below I get errors:


    What am I doing wrong? I would prefer to avoid putting =today() in a cell and refer to it. Is that even possible?

  • Re: Today() In Vba Code


    In VBA use "Date" to get current date, not "Today".


    Code
    'x = date(today())
    'replace the code above with the following:
    x=Date
  • Re: Today() In Vba Code


    OK It actually doesnt work. Datemin is empty. Function doesn't get called.
    Apologies, looks like I still need help!

  • Re: Today() In Vba Code


    Thanks!
    Below is the code that REALLY works! I even have the calendar coming up if the date is out of range to re-select


    Quote from turtle44

    In VBA use "Date" to get current date, not "Today".


    Code
    'x = date(today())
    'replace the code above with the following:
    x=Date

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!