0

I have code to convert a date serial into UK Formatted short dates.

I am calling the module via a C# program and I am getting an American formatted date.

Application.ScreenUpdating = False

Dim x As Workbook
Set x = ActiveWorkbook

Dim WB As Workbook
Workbooks.Open "\\Csdatg04\psproject\Robot\Project Preload\Transactions\Robot WIP\Transactions.csv"
Set WB = ActiveWorkbook
WB.Worksheets(1).Range("J:J").EntireColumn.NumberFormat = "DD/MM/YYYY"
Application.DisplayAlerts = False
x.Save
WB.Save
WB.Close  

How can I force the format to UK dates or alternatively check the dates are formatted in the UK format and, if not, convert the outcome from American to UK?

cybernetic.nomad
  • 6,100
  • 3
  • 18
  • 31
SB999
  • 197
  • 1
  • 1
  • 12
  • Kindly refer similar problems 1 https://stackoverflow.com/questions/52754387/date-type-data-gets-changed-from-dd-mm-to-mm-dd-type-while-importing-tab-del. 2. https://stackoverflow.com/questions/52655007/vba-date-formatting-issue – Ahmed AU Oct 15 '18 at 00:07
  • May try answer by @Rory in my 1st link. – Ahmed AU Oct 15 '18 at 01:04
  • So your original date format was like MMDDYYYY? and you'd like to change them to DD-MM-YYYY? – MT32 Oct 15 '18 at 01:45

1 Answers1

0

Here's one way to deal with fixing dates (in ColA here)

Sub ImportAndFixDates()

    Dim wb As Excel.Workbook, c As Range, v, arr

    Set wb = Workbooks.Open("C:\Users\UserHere\Desktop\tmp.csv")

    With wb.Sheets(1)
        For Each c In .Range(.Range("A2"), .Cells(.Rows.Count, 1).End(xlUp)).Cells
            v = c.Text
            If v Like "#*/#*/#*" Then Then
                c.NumberFormat = "d/m/yyyy"
                arr = Split(v, "/")
                c.Value = Join(Array(arr(1), arr(0), arr(2)), "/")
            End If
        Next c
    End With

End Sub
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • I went through similar problem. I think it will work correctly for dates with day <13. But in [my case]( https://stackoverflow.com/questions/52754387/date-type-data-gets-changed-from-dd-mm-to-mm-dd-type-while-importing-tab-del) dates with day>12 got imported as string only. – Ahmed AU Oct 15 '18 at 00:46