code to extract text from string

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,602
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

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
are you able to use PowerQuery (Get&Transform)?

example:
Column1Column2Column1List.1List.2
18/05/2019 14:40​
18/05/2019 21:57​
18/05/201914:40:0021:57:00
 
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,214,641
Messages
6,120,685
Members
448,977
Latest member
dbonilla0331

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