The following statements were used previously:
strFormatDate = strDay & "/" & strMonth & "/" & strYear
masterworkbook.Sheets("shipper").Cells(7, 8).Value = strFormatDate
Clearly, strFormatDate is a string but conforming to the right Date
format. Together with Cells(7, 8) also with the right Date
format(dd/mm/yyyy), it should do as supposed.
I intend to agree with the US-centric feature(inherited from US
culture? ;-) of VBA as pointed out. Nevertheless, I am still amazed by
how powerful and penetrating it is in that it could manage to transform
"03/04/2006"(in April) to "04/03/2006"(in March) without any knowledge
backing up such an arbitrary transform?? Not weirdly enough, it does
know it should keep "26/04/2006" as it is(tried myself)!!
Thanks Ron and regards
Frank
It'd be interesting to figure out exactly what's going on.
I did some testing, changing my regional settings to d/mm/yyyy as yours, and
ran the Sub below.
It suggests that when VBA writes a string to an Excel cell, the result is
US-Centric. But in circumstances where true dates are being referenced, it
interprets the dates per the Regional Settings:
Run 1 with d/mm/yyyy
Windows Regional Date Settings: day-month-year
VBA: 04-Mar-2006
A1: 03-Apr-2006
A2: 04-Mar-2006
Run 2 with US Settings
Windows Regional Date Settings: month-day-year
VBA: 03-Apr-2006
A1: 03-Apr-2006
A2: 03-Apr-2006
So it seems that if VBA writes a "true date" to Excel, the conversion of the
string is in accord with the regional settings. But if VBA writes a "string
date" to Excel, the conversion to a date will be US-centric.
In the Sub, before writing to A2, I converted the date in VBA from the
concatenated string to a true date with the CDate function, as you can see
below. (In A1, I merely wrote the concatenated string).
===========================================
Option Explicit
Sub TestDate()
Dim strRegSettings As String
Dim strFormatDate As String
Const strDay As String = "4"
Const strMonth As String = "3"
Const strYear As String = "2006"
strFormatDate = strDay & "/" & strMonth & "/" & strYear
Select Case Application.International(xlDateOrder)
Case Is = 0
strRegSettings = "month-day-year"
Case Is = 1
strRegSettings = "day-month-year"
Case Is = 2
strRegSettings = "year-month-day"
Case Else
strRegSettings = "undefined"
End Select
Debug.Print "Windows Regional Date Settings: " & strRegSettings
Debug.Print "VBA: " & Format(strFormatDate, "dd-mmm-yyyy")
Range("A1").Value = strFormatDate
Range("A1").NumberFormat = "dd-mmm-yyyy"
Debug.Print "A1: " & Range("A1").Text
Range("A2").Value = CDate(strFormatDate)
Range("a2").NumberFormat = "dd-mmm-yyyy"
Debug.Print "A2: " & Range("a2").Text
End Sub
=======================================
--ron