converting number to date format

GirishDhruva

Active Member
Joined
Mar 26, 2019
Messages
308
Hi everyone,

can anyone suggest me that can we convert 8 digits or 7 digits numbers to date format

Like example:
12022018 to 12/02/2018
7022018 to 07/02/2018


can this be done using vba, if yes please suggest me how.
Thanks in advance
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Formula solution is quicker

=TEXT(A1,"00-00-0000")+0

or VBA (though am no expert in this)

Code:
Sub NumToDate()
Dim var1 As Date
var1 = Format(Range("A1"), "##-##-####")
MsgBox var1
End Sub
 
Upvote 0
Thanks for you effort...
I found the solution
Code:
Sub Convert_To_Date()
    Dim mydate, mymonth, myday, myyear As String
    Dim row As Long
    row = 2
    Do While Cells(row, 15).Value <> ""
        mydate = CStr(Cells(row, 15))
        If Len(mydate) = 7 Then
            myday = Left(mydate, 1)
            mymonth = Mid(mydate, 2, 2)
            myyear = Right(mydate, 4)
            Cells(row, 15).NumberFormat = "dd/mm/yyyy"
            Cells(row, 15).Value = CDate(myday & "-" & mymonth & "-" & myyear)
        ElseIf Len(mydate) = 8 Then
            myday = Left(mydate, 2)
            mymonth = Mid(mydate, 3, 2)
            myyear = Right(mydate, 4)
            Cells(row, 15).NumberFormat = "dd/mm/yyyy"
            Cells(row, 15).Value = CDate(myday & "-" & mymonth & "-" & myyear)
        End If
    row = row + 1
    Loop
End Sub
 
Upvote 0
Formula solution is quicker

=TEXT(A1,"00-00-0000")+0

Hey, I tried this for myself and it returned the serial number, when I removed the +0 it gave me date format as dd-mm-yyyy which is fine.

The "/" format can be achieved using the escape character \ so essentially you get
=TEXT(A1, "00\/00\/0000")

It looks a bit funny but that's Excel for you!
 
Upvote 0
+0 forces it into an Excel date otherwise it will still be 12022018 and you wont be able to perform any date arithmetic if needed.
You just need to format the cell to display as you want.
 
Upvote 0
Hi, here is another VBA option you can try.

Code:
Sub Convert_To_Date()
With Range("O2:O" & Range("O" & Rows.Count).End(xlUp).Row)
    .Value = Evaluate("CHOOSE({1},0+TEXT(" & .Address & ",""00-00-0000""))")
    .NumberFormat = "dd/mm/yyyy"
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,985
Members
452,540
Latest member
haasro02

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top