Forum

Is it possible to c...
 
Notifications
Clear all

Is it possible to change the date format that is picked up from an input box run from a macro?

5 Posts
4 Users
0 Reactions
82 Views
(@flipper)
Posts: 2
New Member
Topic starter
 

Hello.  I have a long standing frustration when using input boxes to capture dates.  I find that unless the date is entered in the long format (eg 25 Jul 2016) in the Input Box it doesn't display properly in the worksheet.  Is there a work around? Ron

 
Posted : 03/09/2016 11:25 am
(@catalinb)
Posts: 1937
Member Admin
 

Hi Flipper,

You're right, there should be more options for inputbox validations.

You can put the inputbox in a loop, and exit the loop only when the input is a valid date. If the user will not type a valid date, he will be running in circles until they manage to type a date 🙂

The good part using this approach is that it will accept many date strings, like January 23, 2016, or 12/12/2016. You control the output by formatting the result of the input box, no matter what date format was typed.

Sub CheckDateInput()
    Dim DateInput As Variant
   
    Do
        DateInput = InputBox("Date:", "Dates Only")
         
        If IsDate(DateInput) Then Exit Do
        If Not IsDate(DateInput) Then MsgBox "Try again, that was not a valid date", vbCritical + vbOKOnly, "Dates Only"
        
    Loop
   
        Sheet1.Cells(1).NumberFormat = "mm/dd/yyyy": Sheet1.Cells(1).Value = DateInput
    
End
 
Posted : 03/09/2016 1:47 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Ron,

You could use a date picker in your form so the date is always in a known format.  No typing required for the user either.

Regards

Phil

 
Posted : 03/09/2016 8:13 pm
(@sunnykow)
Posts: 1417
Noble Member
 

Dates are pretty tough to handle given the different formats (US/UK formats) and selecting them from a list is the best as mentioned by Philip.

I normally allow users to enter the dates with some validation using a userform. Here is a simple userform to share with you that validate both dates and numbers.

 
Posted : 04/09/2016 1:04 am
(@flipper)
Posts: 2
New Member
Topic starter
 

Excellent, three very useful tips for handling dates thank you.  I'm not sure which will suit me best right now so will give all three a try.  The lack of control over the InputBox formatting is often frustrating, especially after designing a visually pleasing and bespoke user form. 

Ron

 
Posted : 04/09/2016 3:20 am
Share: