VBA wrong date format when setting cell value

martipe1

Board Regular
Joined
Mar 16, 2007
Messages
71
I ask through VBA for starting and end dates and set the value in 2 different cells, my problem is that one value is saved as mm/dd/yy and the other one as dd/mm/yy.

My code is:

Sub DateReport()

Dim StartDate, EndDate As Date

StartDate = InputBox("Report Starting Date (dd/mm/aa)", "Report", Format(Now(), "dd/mm/yy"))
StartDate = Format(CDate(StartDate), "dd/mm/yyyy")
Range("StartReport").Value = StartDate

EndDate = InputBox("Report End Date (dd/mm/aa)", "Report", Format(StartDate, "dd/mm/yy"))
EndDate = Format(CDate(EndDate), "dd/mm/yyyy")
Range("EndReport").Value = EndDate

End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I'm not sure I understand your problem, but maybe this:

VBA Code:
Sub DateReport()
    Dim StartDate, EndDate As Date
    
    StartDate = InputBox("Report Starting Date (dd/mm/aa)", "Report", Format(Now(), "dd/mm/yy"))
    StartDate = Format(CDate(StartDate), "dd/mm/yyyy")
    With Range("StartReport")
        .NumberFormat = "mm/dd/yy"
        .Value = StartDate
    End With
    
    EndDate = InputBox("Report End Date (dd/mm/aa)", "Report", Format(StartDate, "dd/mm/yy"))
    EndDate = Format(CDate(EndDate), "dd/mm/yyyy")
    With Range("EndReport")
        .NumberFormat = "mm/dd/yy"
        .Value = EndDate
    End With
End Sub
 
Upvote 0
Thank you for your comments.

With my code the start date format was mm/dd/yy and end date dd/mm/yy

After I included the ñines that you commented, the result is the opposite, start date format is dd/mm/yy and end date mm/dd/yy

Have no idea why the format changes when setting cell value
 
Upvote 0
Sorry, but was wrong still same result

First date format is mm/dd/yy and second dd/mm/yy

I changed your code to format mmmm-yyyy

The default is today's meaning I get for first date July 2021 and for second September 2021
 
Upvote 0
How about:
Note: I changed the range
VBA Code:
Sub DateReport2()
    Dim StartDate As Date, EndDate As Date
 
    StartDate = CDate(InputBox("Report Starting Date (dd/mm/aa)", "Report", Format(Now(), "dd/mm/yy")))
    With Range("A1")
        .NumberFormat = "mm/dd/yy"
        .Value = StartDate
    End With
 
    EndDate = CDate(InputBox("Report End Date (dd/mm/aa)", "Report", Format(StartDate, "dd/mm/yy")))
    With Range("b1")
        .NumberFormat = "mm/dd/yy"
        .Value = EndDate
    End With
End Sub

Edit:
Actually your Dim statement is wrong, it should be:
Dim StartDate As Date, EndDate As Date
So amend your Dim statement in your original code (post #1), see what happens.
 
Last edited:
Upvote 0
Solution
Dim StartDate, EndDate As Date would mean

StartDate as Variant
EndDate as Date

that is why @Akuini said the statement is wrong :)
 
Upvote 0
How about:
Note: I changed the range
VBA Code:
Sub DateReport2()
    Dim StartDate As Date, EndDate As Date
 
    StartDate = CDate(InputBox("Report Starting Date (dd/mm/aa)", "Report", Format(Now(), "dd/mm/yy")))
    With Range("A1")
        .NumberFormat = "mm/dd/yy"
        .Value = StartDate
    End With
 
    EndDate = CDate(InputBox("Report End Date (dd/mm/aa)", "Report", Format(StartDate, "dd/mm/yy")))
    With Range("b1")
        .NumberFormat = "mm/dd/yy"
        .Value = EndDate
    End With
End Sub

Edit:
Actually your Dim statement is wrong, it should be:
Dim StartDate As Date, EndDate As Date
So amend your Dim statement in your original code (post #1), see what happens.
Thank you!!
the Dim statement was the issue
 
Upvote 0
Dim StartDate, EndDate As Date would mean

StartDate as Variant
EndDate as Date

that is why @Akuini said the statement is wrong :)
Thank you for your answer!!
yes, that was the issue
Sorry, I couldn't tag your answer as a solution as only one is allowed
 
Upvote 0
Thank you for your answer!!
yes, that was the issue
Sorry, I couldn't tag your answer as a solution as only one is allowed
The solution belongs to @Akuini ;) . I was just explaining more detail on the reason why. I see many people declare variables like that which is not correct for VBA but maybe okay for other language. :)
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,315
Members
449,081
Latest member
tanurai

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