VBA Text to Time

tezza

Active Member
Joined
Sep 10, 2006
Messages
398
Office Version
  1. 365
Platform
  1. Windows
  2. Web
Hi all,

I got this report that comes from an external software which is showing time as text in a 12 hour format, eg: 04:30 PM

I can add a formula to change it so that it works = TimeValue(C2)

How do I do this in VBA:
  • Insert Col at Col D
  • Cell D1 = Time
  • Cells D2 down = TimeValue(C2) if C2 isn't blank (drag down until it gets to the bottom row
  • Change Range Col D2 down to Time Format [hh]:mm
Note: Col A always has data in it to know when to stop dragging the formula down.

In a formula this works, but other people use it that would likely mess it up, so I'll run it as a macro.

Excel Formula:
IF(C2="","",TIMEVALUE(C2))

EG:
Staff_Mileage (2).xlsx
CD
1Start TimeTime
2 
3 
4 
5 
6 
7 
808:25 AM08:25
9 
10 
1109:00 AM09:00
1209:00 AM09:00
13 
14 
1504:30 PM16:30
Staff_Mileage
Cell Formulas
RangeFormula
D2:D15D2=IF(C2="","",TIMEVALUE(C2))
 
Hello,
I think the macro below should do the work:
VBA Code:
Public Sub AddTimeCol()
  Dim colA As Variant
  With ThisWorkbook.Worksheets("Staff_Mileage")
    .Columns("B").Insert xlShiftToRight
    .Range("B1").Value2 = "Time"
    colA = Range(.Range("A2"), .Cells(.Rows.Count, 1).End(xlUp)).Value2
  End With
  
  Dim i As Long
  On Error Resume Next
  For i = LBound(colA, 1) To UBound(colA, 1)
    If IsEmpty(colA(i, 1)) Then
      colA(i, 1) = ""
    Else
      colA(i, 1).Value2 = VBA.TimeValue(colA(i, 1))
    End If
  Next i
  On Error GoTo 0
  
  With ThisWorkbook.Worksheets("Staff_Mileage").Range("B2").Resize(UBound(colA) - LBound(colA) + 1)
    .Value2 = colA
    .NumberFormat = "[hh]:mm"
  End With
End Sub
 
Upvote 0
Hiya, thank you for your reply.

Something is not quite right though as it's pulling date and time?

Screenshot 2025-03-27 134851.png
 
Upvote 0
Oh that's curious, i did not have this on my test.

What if you edit the code like so:

VBA Code:
Public Sub AddTimeCol()
  Dim colA As Variant
  With ThisWorkbook.Worksheets("Staff_Mileage")
    .Columns("B").Insert xlShiftToRight
    .Range("B1").Value2 = "Time"
    colA = Range(.Range("A2"), .Cells(.Rows.Count, 1).End(xlUp)).Value2
  End With
  
  Dim i As Long
  On Error Resume Next
  For i = LBound(colA, 1) To UBound(colA, 1)
    If IsEmpty(colA(i, 1)) Then
      colA(i, 1) = ""
    Else
      colA(i, 1).Value2 = TimeSerial(Hour(colA(i, 1)), Minute(colA(i, 1)), 0)
    End If
  Next i
  On Error GoTo 0
  
  With ThisWorkbook.Worksheets("Staff_Mileage").Range("B2").Resize(UBound(colA) - LBound(colA) + 1)
    .Value2 = colA
    .NumberFormat = "[hh]:mm"
  End With
End Sub
 
Upvote 0
Oh that's curious, i did not have this on my test.

What if you edit the code like so:

VBA Code:
Public Sub AddTimeCol()
  Dim colA As Variant
  With ThisWorkbook.Worksheets("Staff_Mileage")
    .Columns("B").Insert xlShiftToRight
    .Range("B1").Value2 = "Time"
    colA = Range(.Range("A2"), .Cells(.Rows.Count, 1).End(xlUp)).Value2
  End With
 
  Dim i As Long
  On Error Resume Next
  For i = LBound(colA, 1) To UBound(colA, 1)
    If IsEmpty(colA(i, 1)) Then
      colA(i, 1) = ""
    Else
      colA(i, 1).Value2 = TimeSerial(Hour(colA(i, 1)), Minute(colA(i, 1)), 0)
    End If
  Next i
  On Error GoTo 0
 
  With ThisWorkbook.Worksheets("Staff_Mileage").Range("B2").Resize(UBound(colA) - LBound(colA) + 1)
    .Value2 = colA
    .NumberFormat = "[hh]:mm"
  End With
End Sub
Have you factored in that I'm referring to columns C and D, not A and B? Col A is purely to know how many rows are populated.
 
Upvote 0
No indeed, i was working with Col A & B. This should be better

VBA Code:
Public Sub AddTimeCol()
  Dim colIni As Variant
  With ThisWorkbook.Worksheets("Staff_Mileage")
    .Columns("D").Insert xlShiftToRight
    .Range("C1").Value2 = "Time"
    colIni = Range(.Range("C2"), .Cells(.Rows.Count, 3).End(xlUp)).Value2
  End With
 
  Dim i As Long
  On Error Resume Next
  For i = LBound(colIni, 1) To UBound(colIni, 1)
    If IsEmpty(colIni(i, 1)) Then
      colIni(i, 1) = ""
    Else
      colIni(i, 1).Value2 = TimeSerial(Hour(colIni(i, 1)), Minute(colIni(i, 1)), 0)
    End If
  Next i
  On Error GoTo 0
 
  With ThisWorkbook.Worksheets("Staff_Mileage").Range("C2").Resize(UBound(colIni) - LBound(colIni) + 1)
    .Value2 = colIni
    .NumberFormat = "[hh]:mm"
  End With
End Sub
 
Upvote 0
Solution
No indeed, i was working with Col A & B. This should be better

VBA Code:
Public Sub AddTimeCol()
  Dim colIni As Variant
  With ThisWorkbook.Worksheets("Staff_Mileage")
    .Columns("D").Insert xlShiftToRight
    .Range("C1").Value2 = "Time"
    colIni = Range(.Range("C2"), .Cells(.Rows.Count, 3).End(xlUp)).Value2
  End With
 
  Dim i As Long
  On Error Resume Next
  For i = LBound(colIni, 1) To UBound(colIni, 1)
    If IsEmpty(colIni(i, 1)) Then
      colIni(i, 1) = ""
    Else
      colIni(i, 1).Value2 = TimeSerial(Hour(colIni(i, 1)), Minute(colIni(i, 1)), 0)
    End If
  Next i
  On Error GoTo 0
 
  With ThisWorkbook.Worksheets("Staff_Mileage").Range("C2").Resize(UBound(colIni) - LBound(colIni) + 1)
    .Value2 = colIni
    .NumberFormat = "[hh]:mm"
  End With
End Sub
Works like a dream, thank you 😃
 
Upvote 0

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