My worksheet has these formulas. Some of the values in column T are Date and some are string or general:
And its values:
I have this VBA code that loops through each cell in a column and convert the string value to a date and format the date. However, it is working perfectly fine with a double-digit month and day, but not single digits. What is the cause of problem and how can I make it to always result in double digit, i.e "01 - 02 -2021", not "1/2/2021"?
Sub Date_format()
Dim Cel As Range
Dim i As Integer
i = ActiveWorkbook.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row
' First I applied mm - dd as the same as the work sheet
ActiveSheet.Range("T2:T" & i).NumberFormat = "mm-dd-yyyy"
For Each Cel In ActiveSheet.Range("T2:T" & i)
If Not Cel.Value = "n/a" Then
' Then I applied dd - mm to swap the day and month
Cel.Value = Format(DateValue(Cel.Value), "dd - mm - yyyy")
End If
Next
End Sub
Once I applied .NumberFormat = "dd-mm-yyyy"
to the range, my other formula =DAYS(TODAY(),T2)
that calculate days, are not working any more on most cells as shown in this picture: