If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Getting dd/mm/yyyy mask to apply to an InputBox

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-18-09, 11:14
CasparV CasparV is offline
Registered User
 
Join Date: Jul 2007
Posts: 54
Getting dd/mm/yyyy mask to apply to an InputBox

I have had a maddenning time getting this to work and so I post this in the hope that it may help someone else with the same frustration (or just make the experts laugh because it looks so easy).

What I wanted was to have a date input by the user during the running of an Excel Macro.

My first attempt was:

DespatchDate = Application.InputBox( _
Prompt:="Expected Despatch Date", _
Default:=Format(Date, "dd/mm/yyyy"))
' Now store whatever is entered
Range("F1").Select
Selection.NumberFormat = "d/m/yyyy"
ActiveCell.FormulaR1C1 = DespatchDate

All worked fine and the InputBox even defaulted to today's date BUT when I tried it with 01/02/2009 (which should be 1st February) it went wrong because the result which appeared in cell F1 was 02/01/2009 (ie 2nd January). After a lot of head-banging I found a very simple solution which I share below:

RepeatGetDate:
DespatchDate = Application.InputBox( _
Prompt:="Expected Despatch Date", _
Default:=Format(Date, "dd/mm/yyyy"), _
Type:=1)
On Error GoTo RepeatGetDate
If DespatchDate = False Then
' This catches if the user enters something invalid like text
GoTo RepeatGetDate
End If
' Now store whatever is entered
Range("F1").Select
Selection.NumberFormat = "d/m/yyyy"
ActiveCell.FormulaR1C1 = DespatchDate

The error trapping helped, but it is the simple statement that Type:=1 that makes the real difference. As I understand it this makes the entry numerical and then Excel handles it correctly - simple or what!

I hope this helps someone else, because I was going to post my marbles here and ask for some new ones - heigh-ho!

Many thanks,
Caspar
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On