formatting data

Hi,

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.

thanks
mcrmgAsked:
Who is Participating?

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

x
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.
0
mcrmgAuthor Commented:
huh...sorry
test--1-.xlsx
0
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

0

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"
    Next
    
End Sub

Open in new window

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

=IF(B2<19000000,B2,DATE(LEFT(B2,4),MID(B2,5,2),RIGHT(B2,2)))

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.
0
Rob HensonFinance AnalystCommented:
Apologies I did not look down column A far enough.

Try this instead.
Column C for column A dates:
=IF(A2="","",IF(A2<19000000,A2,DATE(LEFT(A2,4),MID(A2,5,2),RIGHT(A2,2))))

Column D for column D dates:
=IF(B2="","",IF(B2<19000000,B2,DATE(LEFT(B2,4),MID(B2,5,2),RIGHT(B2,2))))

Copy and paste values as before.
0
mcrmgAuthor Commented:
thank you very much
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome!
0
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
VBA

From novice to tech pro — start learning today.