Consulting

Results 1 to 7 of 7

Thread: Solved: Help to solve a little mistery

  1. #1
    VBAX Regular
    Joined
    Apr 2013
    Posts
    12
    Location

    Solved: Help to solve a little mistery

    I'm trying to ask for a date and provide as default today's date. I tried with the following

    Date= Application.InputBox("date (month/day/year)", "date", now(), , , , , 1)

    worked fine. Then since it shows also the time, which is not needed I changed now() with today() as shown below and it tells me

    compile error:
    Sub or Function not defined

    Date= Application.InputBox("date (month/day/year)", "date", TODAY(), , , , , 1)

    What am I doing wrong?

    Thanks a lot

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    try this
    [VBA]Date = Application.InputBox("date (month/day/year)", "date", Format(Now(), "mm/dd/yyyy"), , , , , 1)
    [/VBA]

  3. #3
    VBAX Regular
    Joined
    Apr 2013
    Posts
    12
    Location
    Tnx JKwan, it worked!!

    so basically today() cannot be used in VBA...?


  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Today() is not a VBA function.

    I would use Date rather than Now() in VBA since time was not needed and avoid using named functions for variable names.

    If you love Today() then use Evaluate().

    [VBA]
    Sub test()
    Dim d As Date
    d = Application.InputBox("date (month/day/year)", "date", Format(Date, "mm/dd/yyyy"), , , , , 1)
    MsgBox Format(d, "mm/dd/yyyy"), , "test"
    End Sub

    Sub test2()
    Dim d As String
    d = Application.InputBox("date (month/day/year)", "date", Format(Evaluate("Today()"), "mm/dd/yyyy"), , , , , 1)
    MsgBox Format(d, "mm/dd/yyyy"), , "test2"
    End Sub[/VBA]

  5. #5
    VBAX Regular
    Joined
    Apr 2013
    Posts
    12
    Location
    Thanks Ken :-).

    So basically one can use Evaluate ("no VBA Funct.") anytime one has a function that cannot be used directly in VBA, correct?

    Thanks again :-)!

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Normally, for a worksheet function, I use WorkSheetFunction.FunctionName(input parameters) or Application.WorksheetFunction.FunctionName(input parameters). For some functions not available via WorkSheetFunction method as in this case, use Evaluate().

    Brackets, [], can be used to directly Evaluate some worksheetfunctions and useful for quick array notation.

  7. #7
    VBAX Regular
    Joined
    Apr 2013
    Posts
    12
    Location
    Thanks a lot!! Very helpful :-)

Posting Permissions

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