Number to Date for multiple columns

GirishDhruva

Active Member
Joined
Mar 26, 2019
Messages
308
Hi Everyone,
Here i am trying to convert multiple columns from numbers to date(Columns are M,N,O)
I tried for converting only one column, but couldn't do the same for multiple columns in one Loop.
Can anyone suggest me how i can do this.......
Code:
    row = 2
    Do While Cells(row, 11).Value <> ""
        mydate = CStr(Cells(row, 13))
        If Len(mydate) = 7 Then
            myday = Left(mydate, 1)
            mymonth = Mid(mydate, 2, 2)
            myyear = Right(mydate, 4)
            Cells(row, 13).NumberFormat = "dd/mm/yyyy"
            Cells(row, 13).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, 13).NumberFormat = "dd/mm/yyyy"
            Cells(row, 13).Value = CDate(myday & "-" & mymonth & "-" & myyear)
        End If
    row = row + 1
    Loop
Thanks in advance
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Doesn't solve your problem but if Len(mydate) is only going to be 7 or 8 you can remove that IF condition and shorten the code to simply

Code:
row = 2
    Do While Cells(row, 11).Value <> ""
        mydate = CStr(Cells(row, 13))

            myday = Left(mydate, 1+(Len(mydate)=8))
            mymonth = Mid(mydate, 2+(Len(mydate)=8), 2)

            myyear = Right(mydate, 4)
            Cells(row, 13).NumberFormat = "dd/mm/yyyy"
            Cells(row, 13).Value = CDate(myday & "-" & mymonth & "-" & myyear)
    row = row + 1
    Loop
 
Last edited:
Upvote 0
This may solve your problem (am no VBA expert)
Loop where there is 13 is referenced

Code:
For i = 13 to 15
row = 2
    Do While Cells(row, 11).Value <> ""
        mydate = CStr(Cells(row, i))

            myday = Left(mydate, 1+(Len(mydate)=8))
            mymonth = Mid(mydate, 2+(Len(mydate)=8), 2)

            myyear = Right(mydate, 4)
            Cells(row, i).NumberFormat = "dd/mm/yyyy"
            Cells(row, i).Value = CDate(myday & "-" & mymonth & "-" & myyear)
    row = row + 1
    Loop
next i
 
Last edited:
Upvote 0
Does this macro do what you want...
Code:
Sub NumToDate()
  With Columns("M:O").SpecialCells(xlConstants, xlNumbers)
    .Value = Evaluate("IF({1},0+TEXT(" & .Address & ",""00\/00\/0000""))")
    .NumberFormat = "dd/mm/yyyy"
  End With
End Sub
 
Upvote 0
Does this macro do what you want...
Code:
Sub NumToDate()
  With Columns("M:O").SpecialCells(xlConstants, xlNumbers)
    .Value = Evaluate("IF({1},0+TEXT(" & .Address & ",""00\/00\/0000""))")
    .NumberFormat = "dd/mm/yyyy"
  End With
End Sub

Thank you for your reply , but for first two columns(M,N) it's displaying as "#Value" errors and only for last column(O) it's displaying correctly
 
Upvote 0
This may solve your problem (am no VBA expert)
Loop where there is 13 is referenced

Rich (BB code):
For i = 13 to 15
row = 2
    Do While Cells(row, i).Value <> ""
        mydate = CStr(Cells(row, i))

            myday = Left(mydate, 1+(Len(mydate)=8))
            mymonth = Mid(mydate, 2+(Len(mydate)=8), 2)

            myyear = Right(mydate, 4)
            Cells(row, i).NumberFormat = "dd/mm/yyyy"
            Cells(row, i).Value = CDate(myday & "-" & mymonth & "-" & myyear)
    row = row + 1
    Loop
next i

Thank you for your reply , but it's throwing me an error in highlighted row as "Type Mismatch"
 
Last edited:
Upvote 0
This may solve your problem (am no VBA expert)
Loop where there is 13 is referenced

Code:
For i = 13 to 15
row = 2
    Do While Cells(row, 11).Value <> ""
        mydate = CStr(Cells(row, i))

            myday = Left(mydate, 1+(Len(mydate)=8))
            mymonth = Mid(mydate, 2+(Len(mydate)=8), 2)

            myyear = Right(mydate, 4)
            Cells(row, i).NumberFormat = "dd/mm/yyyy"
            Cells(row, i).Value = CDate(myday & "-" & mymonth & "-" & myyear)
    row = row + 1
    Loop
next i
But not even Column 13 is getting printed
 
Upvote 0
Thank you for your reply , but for first two columns(M,N) it's displaying as "#Value" errors and only for last column(O) it's displaying correctly

That would mean the values in Columns M and N are not numbers. Perhaps they have a space character at the end or, if you got the values from the web, perhaps they have a non-breaking space at the end. See if this version works...
Code:
Sub NumToDate()
  With Columns("M:O")
    .Replace " ", "", xlPart, , , , False, False
    .Replace Chr(160), "", xlPart, , , , False, False
    With .SpecialCells(xlConstants, xlNumbers)
      .Value = Evaluate("IF({1},0+TEXT(" & .Address & ",""00\/00\/0000""))")
      .NumberFormat = "dd/mm/yyyy"
    End With
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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