I'm having trouble changing to date format. I have cells containing different dates but not all of them are formatted as date. One of them is "yy-mm-dd", for example "13-04-08", but it is formatted as General. I'm using this code:
xCell = Format(xCell, "yyyy-mm-dd")
Problem is that excel cannot tell if "13" or "08" is the "yyyy" so it doesn't change anything. How do I solve this? Do I need to tell excel which numbers are year before it changes the date, and how do I do that? Can it be included in the Format method?
EDIT: I think I need to explain the whole thing as the problem seems to lay somewhere else. The cells containing the dates look like this from start, being formatted as General:
13-05-06 A
13-05-21 A
...
I remove the unwanted 'A' with this code:
Sub rensa()
Dim Found As Range
For Each xCell In Range("D2:D999")
If IsEmpty(xCell.Value) Then Exit For
Set Found = xCell.Find(What:="A", LookIn:=xlValues, LookAt:=xlPart)
If Found Is Nothing Then
Else
xCell.Value = Left(xCell.Value, Len(xCell.Value) - 1)
End If
Next xCell
End Sub
I have tried these codes to set the cell format to date:
Range("D2:D999").NumberFormat = "yyyy-mm-dd"
Range("D2:D999").NumberFormat = "m/d/yyyy"
I have also tried to implement them in the For loop like this:
Sub rensa()
Dim Found As Range
For Each xCell In Range("D2:D999")
If IsEmpty(xCell.Value) Then Exit For
Set Found = xCell.Find(What:="A", LookIn:=xlValues, LookAt:=xlPart)
If Found Is Nothing Then
xCell.NumberFormat = "yyyy-mm-dd"
Else
xCell.Value = Left(xCell.Value, Len(xCell.Value) - 1)
xCell.NumberFormat = "yyyy-mm-dd"
End If
Next xCell
End Sub
But that didn't work as I wanted either. Everything makes the result look like this, still formatted as General:
13-05-06
13-05-21
...
So the A is gone, but nothing else changes.