Isolate Times from Custom Date

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good evening, I am trying to figure out how I can isolate a 24 hour time from a Custom Date format (dd mmm yyyy hhmm) in Excel using VBA. Image A illustrates Column D prior to my VBA. Image B illustrates what happens when I use the VBA. Image C is how I would like it to appear. I'm sure a need a code that will change the format to text in order to keep the leading 0. Also I need to be mindful to have it change only the cells with times. I want to avoid having Value errors in the blank columns. Lastly if it's not too difficult, a code I can use to show there are no errors would be great.
Thank you,

VBA Code:
[ATTACH type="full"]24937[/ATTACH]Sub IsolateTime()
    Dim cel As Range

    Range("D2").Select
    Range(Selection, Selection.End(xlDown)).Select
    
    For Each cel In Application.Selection.Cells
        cel.Value = Right(cel.Text, 4)
    Next cel

    Selection.NumberFormat = "@"
End Sub
 

Attachments

  • Final Time.JPG
    Final Time.JPG
    61.8 KB · Views: 13

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
You can extract the time from a cell which contains date and time using this formula:
=D2-INT(D2)
Then format the cell as time however you want it displayed.
So you don't need to use vba. If you really want to use vba you can use the same idea. this just relies on the fact that excel stores dates and times as numbers where the integers are days and the decimal part is the time as a fraction of a day.
 
Upvote 0
You can extract the time from a cell which contains date and time using this formula:
=D2-INT(D2)
Then format the cell as time however you want it displayed.
So you don't need to use vba. If you really want to use vba you can use the same idea. this just relies on the fact that excel stores dates and times as numbers where the integers are days and the decimal part is the time as a fraction of a day.
This is one of 20 VBAs I have for this particular project, and multiple people will be using moreover, I need to have so the cells can be edited. I definitely want to keep this as a VBA. Thank you, for your input.
 
Upvote 0
if you want to do it with vba this code will do it using the same technique:
VBA Code:
  Dim cel As Range
   lastrow = Cells(Rows.Count, "D").End(xlUp).Row
   inarr = Range(Cells(2, 4), Cells(lastrow, 4))
    For i = 1 To lastrow - 1
     inarr(i, 1) = inarr(i, 1) - Int(inarr(i, 1))
    Next i
    Range(Cells(2, 4), Cells(lastrow, 4)) = inarr
    Range(Cells(2, 4), Cells(lastrow, 4)).NumberFormat = "h:mm"
Note I load all the data into a variant array because it is faster and I just hate looping through a worksheet
 
Upvote 0
if you want to do it with vba this code will do it using the same technique:
VBA Code:
  Dim cel As Range
   lastrow = Cells(Rows.Count, "D").End(xlUp).Row
   inarr = Range(Cells(2, 4), Cells(lastrow, 4))
    For i = 1 To lastrow - 1
     inarr(i, 1) = inarr(i, 1) - Int(inarr(i, 1))
    Next i
    Range(Cells(2, 4), Cells(lastrow, 4)) = inarr
    Range(Cells(2, 4), Cells(lastrow, 4)).NumberFormat = "h:mm"
Note I load all the data into a variant array because it is faster and I just hate looping through a worksheet
Thank you for getting back to me on this. I tried the code you suggested and I ended up with a Run-time error '13' Type Mismatch. I enclosed an image. Once this VBA is working properly I'll move it within the Module. Many thanks.
 

Attachments

  • Mismatch.JPG
    Mismatch.JPG
    86.9 KB · Views: 10
Upvote 0
That implies that the value in column D is not a time valued stored as a number as per the EXCEl format. Use debug to look at the value of I and you can then see if it is just one row or all rows.
 
Upvote 0
That implies that the value in column D is not a time valued stored as a number as per the EXCEl format. Use debug to look at the value of I and you can then see if it is just one row or all rows.
I know it’s in a custom format dd mmm yyyy hhmm.
 
Upvote 0
try this code that check to see if the cell contains text:
VBA Code:
Sub test()
  Dim cel As Range
  Dim myrange As Range
   lastrow = Cells(Rows.Count, "D").End(xlUp).Row
   Set myrange = Range("d2:d" & lastrow)
    For Each cel In myrange.Cells
        If Not (Application.IsText(cel)) Then
         cel.Value = cel.Value - Int(cel.Value)
        Else
         MsgBox (cel.Row & " is not an excel date time format")
        End If
     Next
    Range(Cells(2, 4), Cells(lastrow, 4)).NumberFormat = "h:mm"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

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