formatting data


Please see the file attached. There are two columns, each column represents date, but the format is inconsistent.  Is there a way to use a simple VBA to look for specific columns and change the value to mm/dd/yyyy.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You forgot to attach the file.
mcrmgAuthor Commented:
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You may try something like this...
Sub FormatDates()
Dim cell As Range
Dim lr As Long
Dim dtStr As String
Application.ScreenUpdating = False
lr = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For Each cell In Range("A2:B" & lr)
    If cell <> "" And IsNumeric(cell.Value) And Evaluate(Len(cell.Text)) > 5 Then
        dtStr = cell.Value
        cell.Value = DateSerial(Left(dtStr, 4), Mid(dtStr, 5, 2), Right(dtStr, 2))
    End If
Next cell
Range("A2:B" & lr).NumberFormat = "mm/dd/yyyy"
Application.ScreenUpdating = True
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Martin LissOlder than dirtCommented:
Add this macro and execute it.

Sub FormatDate()
    Dim cel As Range
    For Each cel In Range("A2:B" & ActiveSheet.UsedRange.Rows.Count)
        If Len(cel) > 5 Then
            If Not IsDate(cel) Then
                cel = Left$(cel, 4) & "/" & Mid$(cel, 2, 2) & "/" & Right$(cel, 2)
            End If
        End If
        cel.NumberFormat = "mm/dd/yyyy"
End Sub

Open in new window

Rob HensonFinance AnalystCommented:
Column A all seem OK but column B is the issue. In column C try this formula, starting in C2:


Copy down as far as required, the values from column C can then be copied and pasted as values into column B.

I have assumed the erroneous dates at the bottom of the column are in yyyymmdd format. If that's not the case then the month and day parts of the DATE function will need swapping.
Rob HensonFinance AnalystCommented:
Apologies I did not look down column A far enough.

Try this instead.
Column C for column A dates:

Column D for column D dates:

Copy and paste values as before.
mcrmgAuthor Commented:
thank you very much
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.