Convert Date and Time Text to an Excel Date/Time Serial Number

wdgor

Board Regular
Joined
Jan 21, 2009
Messages
90
Office Version
  1. 365
Platform
  1. Windows
Any ideas on how I could convert this text string into a date/time serial number in Excel? Thanks for any assistance.

12:09:01AM Jun 18, 2022
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
This looks like a really elegant solution but I have never used power query. I have Excel 365 and power query does show up under the data tab. When I select it and try to use data sources in the current workbook, I get "We didn't find any data sources in this workbook". I originally tried to parse out the data table (see below) thinking that I might be able to do something with it - no luck. Perhaps I need to do some YouTube investigation on how to use power query.
Select the data, on Get & Transform choose from table/range.
Confirm my data has headers.
You can also do this in another workbook, same routine but select data from file from workbook.
 
Upvote 0
With a simple macro:
VBA Code:
Sub Demo()
Application.ScreenUpdating = False
Dim r As Long
With ActiveSheet
  For r = 1 To .UsedRange.Rows.Count
    .Cells(r, 2).Value = CDate(.Cells(r, 1).Text)
  Next
  .Columns(2).NumberFormat = "0.000000"
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
With a simple macro:
VBA Code:
Sub Demo()
Application.ScreenUpdating = False
Dim r As Long
With ActiveSheet
  For r = 1 To .UsedRange.Rows.Count
    .Cells(r, 2).Value = CDate(.Cells(r, 1).Text)
  Next
  .Columns(2).NumberFormat = "0.000000"
End With
Application.ScreenUpdating = True
End Sub
The OP seems content with the formula solution I offered, but if a VBA solution was desired, I would probably do it this way (given the OP has told us the time values have leading zeros when the hours are single digits) since it would eliminate the loop...
VBA Code:
Sub Demo()
  With Range("A1", Cells(Rows.Count, "A").End(xlUp))
    .Offset(, 1) = Evaluate(Replace("0+MID(@&"" ""&REPLACE(@,9,0,"" ""),12,LEN(@)+1)", "@", .Address))
  End With
End Sub
 
Upvote 0
Thanks all but I have implemented Rick’s approach and it works perfectly.
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,825
Members
449,190
Latest member
rscraig11

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