0

I have dates in my excel file in the following format MM/DD/YYYY

I am going through worksheet using for each loop and getting value by cell.Value

I'd like to check whether currently processed cell is in the above format and if so, change it to YYYY-MM-DD ( database format )

Can I do this in VBA ?

Thanks in advance,

1
  • Those are real dates or dates as strings? I asked this because Excel treats dates as numbers so how you see a date in a cell is due to the formatting while there is a number underneath the date. To check if the date is real date not a date string, assuming you have date in A2, then in any blank cell type the formula =ISNUMBER(A2). A2 contains a real date if the formula returns True else it is a date string. Commented Apr 21, 2018 at 14:16

2 Answers 2

1

Without Regex:

Sub DateTest()
    With ActiveCell
        If IsDate(.Value) And .NumberFormat = "mm/dd/yyyy" Then
            .NumberFormat = "yyyy-mm-dd"
        End If
    End With
End Sub

NOTE:

If the values are Strings, changing the NumberFormat will have no visible effect on the cells.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't need regex

Option Explicit

Sub ChangeDateFormats()
    Dim thisCell As Range
    For Each thisCell In ActiveSheet.UsedRange
        Debug.Print thisCell.NumberFormat
        If thisCell.NumberFormat = "m/d/yyyy" Then
            thisCell.NumberFormat = "yyyy-mm-dd"
        End If
    Next thisCell
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.