We help IT Professionals succeed at work.
Get Started

Excel Date input

swjtx99 asked
Last Modified: 2015-03-02

I have the following code that inserts today's date into an input box. The problem is that it's defined to 10 characters which would be max for MM/DD/YYYY but when it's 3/1/2015, that is only eight characters so the ninth will be blank but the  10th will be the hour example: 3/1/2016 9  (if it's 9 am or pm)

That's messing things up considerably, Any way to define the format ?

Sub Step_2_InputTargetDate()

    Dim InputDate As String, InputPrompt As String
    Dim Holidays() As Variant, i As Integer, j As Integer
    Dim ws As Worksheet
    Set ws = Worksheets("Holidays")
    Holidays = ws.Range("Holidays")
    Sheets("Backlog Report").Select
    'Input the target date
    InputPrompt = "Input a target date"
    InputDate = Left(CStr(Now), 10) 'Here 10 characters are too many if the date can be expressed with 8 characters
        InputDate = InputBox(InputPrompt, "Target date", InputDate)
        If Len(InputDate) = 0 Then
            MsgBox "No value, program end"
        End If
        On Error Resume Next
        targetDate = DateValue(InputDate)
        On Error GoTo 0
        If targetDate = 0 Then
            InputPrompt = "Not a valid date, try again"
            If Weekday(targetDate, vbMonday) = 6 Then
                InputPrompt = "Date is a Saturday, try again"
                targetDate = 0
            ElseIf Weekday(targetDate, vbMonday) = 7 Then
                InputPrompt = "Date is a Sunday, try again"
                targetDate = 0
                j = 0
                For i = LBound(Holidays, 1) To UBound(Holidays, 1)
                    If targetDate = Holidays(i, 1) Then
                        j = 1
                    End If
                Next i
                If j = 1 Then
                InputPrompt = "Date is a Holiday, try again"
                targetDate = 0
                End If
            End If
        End If
    Loop While targetDate = 0

Thanks in advance,

Watch Question
Grand Poobah
Most Valuable Expert 2011
Top Expert 2011
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE