Consulting

Results 1 to 5 of 5

Thread: Date Format Issue from Input Box

  1. #1

    Date Format Issue from Input Box

    Hi all,
    I'm using the following code to get the user to enter the Week commencing date of a report into cell K12 of the Performance_Report sheet. The macro works fine apart from returning the year as 1900. I've searched all over the net and cant find a reason why this is happening - can anyone help?

    Sub Date_Entry()
    Dim Request As String
    Dim ReportTitle As String
    Dim DefValue As Date
    Dim ReportDate As Date

    Request = "Please enter the Week Commencing Date"
    ReportTitle = "CEC Weekly Performance Report"
    DefValue = Date - 7
    ReportDate = Val(InputBox(Request, ReportTitle, DefValue))
    Sheets("Performance_Report").Select
    Range("K12").Value = ReportDate
    Selection.NumberFormat = "dd mmmm yy"
    End Sub

  2. #2
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    Welcome to the forum- it's always good to see new members!

    When you post code, please wrap it in VBA tags- it will format it according to VBIDE, which makes it easier to read.

    The return from an input box is of the string data type. I noticed that you tried to use the Val function to get the date, but the Formt function would be a better fit for you because it actually formats your date. Val is a bad fit for what you're trying to do, as it is designed to only pull the numbers out of a text string. I also set ReportDate to a string format- left as Date format it would overwrite the special formatting that is being applied.
    I also removed the selection from your code- selection can cause code slowdowns because it causes changes and events in the UI.

    [vba]Sub DateTest()
    Dim Request As String
    Dim ReportTitle As String
    Dim DefValue As Date
    Dim ReportDate As String

    Request = "Please enter the Week Commencing Date"
    ReportTitle = "CEC Weekly Performance Report"
    DefValue = Date - 7
    ReportDate = Format(CDate(InputBox(Request, ReportTitle, DefValue)), "dd mmmm yy")
    ActiveWorkbook.Sheets(1).Range("K12").Value = ReportDate
    End Sub
    [/vba]

    HTH
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub Date_Entry()
    Dim Request As String
    Dim ReportTitle As String
    Dim DefValue As Date
    Dim ReportDate As Date

    Request = "Please enter the Week Commencing Date"
    ReportTitle = "CEC Weekly Performance Report"
    DefValue = Date - 7
    ReportDate = CDate(InputBox(Request, ReportTitle, DefValue))
    With Sheets("Performance_Report").Range("K12")

    .Value = ReportDate
    .NumberFormat = "dd mmmm yy"
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    The user can put anything in that so it is no wonder you get odd results.

    I would recommend a date control method. You can use this as an example:
    http://vbaexpress.com/forum/showthread.php?p=176132

  5. #5
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    Quote Originally Posted by Kenneth Hobs
    The user can put anything in that so it is no wonder you get odd results.

    I would recommend a date control method. You can use this as an example:
    http://vbaexpress.com/forum/showthread.php?p=176132
    That is a much better method, but if there is some reason that you cannot use it, then you should add some validation code to your routine- especially the IsDate function. Take a look at it in Excel Help for all the details.
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


Posting Permissions

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