简体   繁体   中英

changing date fromat from mm/dd/yyyy to dd/mm/yyyy VBA

So I know this question has been asked a couple of times over, but I believe my situation is a bit different (happy to be proven wrong of course!)

Here is the data flow: a user types a date in a date in a form. They then click a button. My macro then takes that date, runs it through the following function:

    Function AddWeekDays(StartDate As Long, Days As Long) As Date
    Dim i As Long
    Dim d As Date

    d = StartDate
    i = 0

    While i < Days
        d = DateSerial(Year(d), Month(d), Day(d) + 1)
        If Weekday(d, vbMonday) < 6 Then
            i = i + 1
        End If
    Wend

    AddWeekDays = d
End Function

Then it formats the date to change it from mm/dd/yyyy to dd/mm/yyyy in the following way:

Dim deadline As Date
Dim deadline_formatted As Date
Dim DateReceived As String
Dim DateConverted As Date
DateReceived = txt_DateReceived.Text
DateConverted = Format(DateReceived, "dd/mm/yyyy")
deadline = AddWeekDays(DateValue((CStr(DateConverted))), 9)
deadline_formatted = Format(deadline, "dd/mm/yyyy")

However, the deadline_formatted value is still coming out in the mm/dd/yyyy format.

As an example, when a user enters 01/05/2017 the program should return deadline_formatted = 12/05/2017 , but it returns deadline_formatted = 05/12/2017

I have tried changing the variable type to string to see if that made a difference (it didn't), and have tried directly converting the deadline variable to the required format by using the following code:

deadline = Format(AddWeekDays(DateValue((CStr(DateConverted))), 9),"dd/mm/yyyy")

which still returns the incorrect format.

Can anybody out there suggest either:

  • How to fix the formatting issue to get the deadline_formatted into the format dd/mm/yyyy OR
  • suggest a "workaround" to flip the "dd" with the "mm" (not ideal obviously, but if it works, it works!)

Thanks for any advice!

The best way to solve this issue is to actually change your computer's default date/time format to match the method used by the users. (In comments it is stated that the users are Australians but your company is US-based, so the default is probably currently set to be the USA's "mm/dd/yyyy" format.)

By ensuring that the computers date/time format is correct, it will allow you to process a date as a Date, and it can be stored in Excel cells as a Date, which then allows any of the Australian users to see it displayed as "dd/mm/yyyy" format while a USA-based colleague would see it displayed as "mm/dd/yyyy".

There is a financial risk to your company caused by forcing users to interact with software using an unfamiliar date system, as accidental entering of dates in the wrong formats can lead to major errors downstream, so it is in the company's best interest to allow you to change the settings to be relevant to the users.

It is not directly related to your problem, however I believe it might fix your issues. The manual calculation of adding week days might be the problem here.

There is a built in function to add workdays . You can include/exclude weekends/holidays. The following code replaces your above mentioned code.

Sub AddWeekDays()
    Dim deadline As Date, deadline_formatted As Date
    deadline = txt_DateReceived.Value
    deadline_formatted = Format(Application.WorksheetFunction.WorkDay(deadline, 9), "dd/mm/yyyy")
    'debug.print deadline_formatted
End Sub

the result to be String.

Dim deadline As Date
Dim deadline_formatted As String '<~~ change string
Dim DateReceived As String
Dim DateConverted As Date

txt_DateReceived = "01/05/2017"

DateReceived = txt_DateReceived
DateConverted = Format(DateReceived, "dd/mm/yyyy")
'deadline = AddWeekDays(DateValue((CStr(DateConverted))), 9)
deadline = AddWeekDays(CLng(DateConverted), 9) '<~~ change Long
deadline_formatted = Format(deadline, "dd/mm/yyyy")

I wouldn't bother about the regional settings. Instead, make sure that all dates are captured as Date() or Now() values (42123 or 42123.5555). On the output side such values can be presented in any format you wish. To ensure that dates are entered correctly my preferred way is to use a date picker. If that can't be done make no rules for entering the date at all, working on the presumption that each user will know how to enter a date on his/her machine. Add a date check, like ISDATE(), which will catch some input errors but not all. You don't need to catch all. You only need to teach users how to input dates on their respective PCs.

With this line you don't need anything else.

Range("E:E").TextToColumns FieldInfo:=Array(0, xlDMYFormat)
'1.2.2019 -> 01/02/2019
'2,3,2019 -> 02/03/2019
'3-4-2019 -> 03/04/2019
'4*5*2019 -> 04/05/2019
'5_-6-*2019 -> 05/06/2019
'and so on

Of course you can change the format with

xlMDYFormat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM