Input Box Help - Validate Date Format

T

Tbal

need to code that will validate that the user has input the date (via
InputBox) in the correct format. The user will be prompted to input the
date in the format: Month (first three letters only) [space] Year
(4-digit year), e.g., Jan 2005.

I am having trouble validating the month format. Any suggestions would
be greatly appreciated. I am sure this is basic VBA but I am feeling
VBA-challenged today! :)
 
D

Dave Peterson

Maybe something like:

Option Explicit
Sub testme()

Dim mCtr As Long
Dim myStr As String
Dim okDate As Boolean
Dim FoundMonth As Boolean

okDate = False

Do
myStr = InputBox(prompt:="enter date: mmm yyyy")

If myStr = "" Then Exit Sub 'user hit cancel

okDate = True
If Len(myStr) <> 8 Then
okDate = False
ElseIf Mid(myStr, 4, 1) <> " " Then
okDate = False
ElseIf IsNumeric(Mid(myStr, 5, 4)) = False Then
okDate = False
Else
FoundMonth = False
For mCtr = 1 To 12
If LCase(Left(myStr, 3)) _
= LCase(Format(DateSerial(2005, mCtr, 1), "mmm")) Then
FoundMonth = True
Exit For
End If
Next mCtr
If FoundMonth = False Then
okDate = False
End If
End If

If okDate = True Then Exit Do
Loop

End Sub

Another option:

You might want to take a look at Ron de Bruin's site for some tips/code/free
calendar control:
http://www.rondebruin.nl/calendar.htm

need to code that will validate that the user has input the date (via
InputBox) in the correct format. The user will be prompted to input the
date in the format: Month (first three letters only) [space] Year
(4-digit year), e.g., Jan 2005.

I am having trouble validating the month format. Any suggestions would
be greatly appreciated. I am sure this is basic VBA but I am feeling
VBA-challenged today! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top