Here is a code snippet in VBA that converts Azerbaijan date format m.d.y
to ar-SA date format d/m/y
. Running the FixDates
with parameter "A" should convert the text m.d.yy hh:mm:ss
to the date value d/m/yyyy hh:mm:ss
for all the cells in column A. It is using RegExp from VBScript class.
Option Explicit
Sub ConvertDates()
Call FixDates("A")
End Sub
Sub FixDates(column As String)
Dim cell As Range
Dim lastRow As Long
lastRow = Range(column & Rows.Count).End(xlUp).Row
For Each cell In Range(column & "1:" & column & lastRow)
If InStr(cell.Value, ".") <> 0 Then
cell.Value = DateValue(RegexReplace(cell.Value, _
"(\d{1,2})\.(\d{1,2})\.(\d{2,4})", "$2/$1/$3"))
End If
cell.NumberFormat = "d/m/yyyy h:mm AM/PM"
Next
End Sub
Function RegexReplace(ByVal text As String, _
ByVal replace_what As String, _
ByVal replace_with As String) As String
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
RE.Pattern = replace_what
RE.Global = True
RegexReplace = RE.Replace(text, replace_with)
End Function
If you need other format, just change one line like this:
cell.NumberFormat = "d/m/yyyy h AM/PM"
I have modified and used this answer and this answer.