Inputbox force use to ude formatt dd/mm/yyyy

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have a macro where the user must inpput a date. I would like this amended so that the date must be entrred as dd/mm/yyy. If not message must appear "Please enter date correctly using dd/mm/yyyy

Code:
 Sub Date_Input()
With Sheets("Statement")

Dim i As Variant
 i = InputBox("Please Enter the Statement Month End for e.g. 31/10/2022")

.Range("AA1").Value = i
.Range("AA2").FormulaR1C1 = "=DATEVALUE(R[-1]C)"
    .Range("AA2").Copy
    .Range("AA1").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    .Range("AA2").ClearContents
    
End With

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
VBA Code:
  If i <>  Format(DateValue(i), "dd/mm/yyyy") Then
    MsgBox "Format must be 'dd/mm/yyyy'"     
  Else     
    .Range("AA1").Value = i
  End If
This may work for you. This morning I was chatting with a colleague about exactly the same issue. How difficult it is, to validate a date according to user's local settings. It needs fiddling around a bit using CDate(), DateValue() and Format() methods.
 
Upvote 0
VBA Code:
  If i <>  Format(DateValue(i), "dd/mm/yyyy") Then
    MsgBox "Format must be 'dd/mm/yyyy'"    
  Else    
    .Range("AA1").Value = i
  End If
This may work for you. This morning I was chatting with a colleague about exactly the same issue. How difficult it is, to validate a date according to user's local settings. It needs fiddling around a bit using CDate(), DateValue() and Format() methods.
Many thanks for the help
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,690
Members
449,092
Latest member
snoom82

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top