Note that there are some explanatory texts on larger screens.

plurals
  1. POExcel not recognizing year when changing to date format with VBA
    primarykey
    data
    text
    <p>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:</p> <pre><code>xCell = Format(xCell, "yyyy-mm-dd") </code></pre> <p>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?</p> <p><strong>EDIT:</strong> 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:</p> <pre><code>13-05-06 A 13-05-21 A ... </code></pre> <p>I remove the unwanted 'A' with this code:</p> <pre><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 </code></pre> <p>I have tried these codes to set the cell format to date:</p> <pre><code>Range("D2:D999").NumberFormat = "yyyy-mm-dd" Range("D2:D999").NumberFormat = "m/d/yyyy" </code></pre> <p>I have also tried to implement them in the For loop like this:</p> <pre><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 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 </code></pre> <p>But that didn't work as I wanted either. Everything makes the result look like this, still formatted as General:</p> <pre><code>13-05-06 13-05-21 ... </code></pre> <p>So the A is gone, but nothing else changes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload