code to extract text from string

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,604
Office Version
  1. 365
Platform
  1. Windows
Hi all

Can someone please help to suggest code for following

through column B I have date/time entries like following..

18-05-19 14:40

..and similar through column C

18-05-19 21:57


I'd like the code to.....
extract the date from the string through column B and place into column E
extract the time from the string through column B and place into column F
extract the time from the string through column C and place into column G


so from above example, the required results in column E, F and G would be..


Column E - 18-05-19
Column F - 14:40
Column G - 21:57

Many thanks in advance
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
are you able to use PowerQuery (Get&Transform)?

example:
[Table="width:, class:head"]
[tr=bgcolor:#FFFFFF][td=bgcolor:#5B9BD5]Column1[/td][td=bgcolor:#5B9BD5]Column2[/td][td][/td][td=bgcolor:#70AD47]Column1[/td][td=bgcolor:#70AD47]List.1[/td][td=bgcolor:#70AD47]List.2[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]
18/05/2019 14:40​
[/td][td=bgcolor:#DDEBF7]
18/05/2019 21:57​
[/td][td][/td][td=bgcolor:#E2EFDA]18/05/2019[/td][td=bgcolor:#E2EFDA]14:40:00[/td][td=bgcolor:#E2EFDA]21:57:00[/td][/tr]
[/table]
 
Last edited:
Upvote 0
Hi all

Can someone please help to suggest code for following

through column B I have date/time entries like following..

18-05-19 14:40

..and similar through column C

18-05-19 21:57
Is there anything else in the cells with those date/time values or is that the only thing in the cells? If that is the only thing, are your entries actual dates formatted to look the way you showed us or are they text strings?
 
Upvote 0
Is there anything else in the cells with those date/time values or is that the only thing in the cells? If that is the only thing, are your entries actual dates formatted to look the way you showed us or are they text strings?

Yes , that's the only thing in the cells and if I click on B5 that shows following

29-05-19 10:33

in the Formula window it shows

29-05-2019 10:33:00 PM
 
Last edited:
Upvote 0
redspanna

Sounds like you have'real' date time values.

Try this.
Code:
Sub ExtractDateAndTime()
Dim arrIn As Variant
Dim I As Long

    arrIn = Range("B2", Range("C" & Rows.Count).End(xlUp)).Value

    ReDim arrOut(LBound(arrIn, 1) To UBound(arrIn, 1), 1 To 3)

    For I = LBound(arrIn, 1) To UBound(arrIn, 1)
        arrOut(I, 1) = DateValue(arrIn(I, 1))
        arrOut(I, 2) = TimeValue(arrIn(I, 1))
        arrOut(I, 3) = TimeValue(arrIn(I, 2))
    Next I

    Range("E2").Resize(UBound(arrOut, 1), 3).Value = arrOut

End Sub
 
Upvote 0
Sorry for late reply Norie, however thanks for your help with the code, works great :)
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,148
Members
453,021
Latest member
Justyna P

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