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
 
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

Hi sir thank you for your reply again and sorry for delay but then also i am getting same error (#Value) ,below is my sample table data
their is no space character in the end

Column MColumn NColumn O
30042018300420187052018
30042018300420187052018
30042018300420187052018
30042018300420187052018
30042018300420187052018
30042018300420187052018

<tbody>
</tbody>
 
Last edited:
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi sir thank you for your reply again and sorry for delay but then also i am getting same error (#Value) ,below is my sample table data
their is no space character in the end
Is there any chance you can post a copy of your original data to DropBox so that we can download it and examine why there is a problem with those two columns?
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,822
Members
449,469
Latest member
Kingwi11y

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