Output Error

iggydarsa

Well-known Member
Joined
Jun 28, 2005
Messages
1,647
Hi everyone,

I have a code looks like this:

Code:
Sub DateOutput()

    Dim startDate As String
    Dim stopDate As String

    startDate = InputBox("Start Date: (dd/mm/yy)")
    startDate = Format(startDate, "dd/mm/yy")

    stopDate = InputBox("Stop Date: (dd/mm/yy)")
    stopDate = Format(stopDate, "dd/mm/yy")

    MsgBox Format(startDate, "dd-mmm-yy")
    MsgBox Format(stopDate, "dd-mmm-yy")

End Sub

For startDate I entered 2/1/05 and msgbox outputted "2-Jan-05" which is fine but after that for stopDate I entered 19/2/05 and msgbox outputted "2-May-19" which is wrong, it should be "19-Feb-05".

What am I doing wrong?
Any suggestions?

Thanks
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

GlennUK

Well-known Member
Joined
Jul 8, 2002
Messages
11,689
Did you paste your code into here? With that code I can't reproduce your problem. It's almost as though you've actually got
Code:
MsgBox Format(stopDate, "yy-mmm-dd")
in your code.
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Your code worked for me, but VBA can be flaky with dates in UK format. Try:

Code:
Sub DateOutput()

    Dim startDate As String
    Dim stopDate As String

    startDate = InputBox("Start Date: (dd/mm/yy)")
    startDate = Format(DateValue(startDate), "dd/mm/yy")

    stopDate = InputBox("Stop Date: (dd/mm/yy)")
    stopDate = Format(DateValue(stopDate), "dd/mm/yy")

    MsgBox Format(startDate, "dd-mmm-yy")
    MsgBox Format(stopDate, "dd-mmm-yy")

End Sub

The DateValue function uses your Control Panel|Regional Options Date setting. What do you have there?
 

iggydarsa

Well-known Member
Joined
Jun 28, 2005
Messages
1,647

ADVERTISEMENT

DateValue() thing didnt work, it gave me the same error.

Right now I am in USA (they use mm/dd/yy) but I am writing this code for someone who is in Turkey (they use dd/mm/yy)

Also funny thing is, how come startDate works perfectly but stopDate doesn't?

Any suggestions?
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
While I agree that it's strange that startDate works perfectly but stopDate doesn't, you can't mix and match US and European date formats. Try:

Code:
Sub DateOutput()
    Dim startDate As String
    Dim stopDate As String
    Dim D As Integer
    Dim M As Integer
    Dim Y As Integer
    startDate = InputBox("Start Date: (dd/mm/yy)")
    startDate = WorksheetFunction.Substitute(startDate, "/", "@", 2)
    D = Left(startDate, InStr(1, startDate, "/") - 1)
    M = Mid(startDate, InStr(1, startDate, "/") + 1, InStr(1, startDate, "@") - InStr(1, startDate, "/") - 1)
    Y = WorksheetFunction.Substitute(startDate, D & "/" & M & "@", "")
    startDate = Format(DateSerial(Y, M, D), "dd-mmm-yy")
    stopDate = InputBox("Stop Date: (dd/mm/yy)")
    stopDate = WorksheetFunction.Substitute(stopDate, "/", "@", 2)
    D = Left(stopDate, InStr(1, stopDate, "/") - 1)
    M = Mid(stopDate, InStr(1, stopDate, "/") + 1, InStr(1, stopDate, "@") - InStr(1, stopDate, "/") - 1)
    Y = WorksheetFunction.Substitute(stopDate, D & "/" & M & "@", "")
    stopDate = Format(DateSerial(Y, M, D), "dd-mmm-yy")
    MsgBox startDate
    MsgBox stopDate
End Sub
 

iggydarsa

Well-known Member
Joined
Jun 28, 2005
Messages
1,647
Thats great Andrew,

It worked :LOL:

Thank you very very very very very very much :pray:
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,413
Messages
5,837,094
Members
430,477
Latest member
roseinsydney

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
Top