Formatting Issues?

en_passant

New Member
Joined
Feb 10, 2016
Messages
13
Hello all,

Hoping for some help with something I can't seem to get my head around. I've taken a screenshot which will hopefully illustrate what I'm up against well, but the issue I'm having is with raw data which is formatted in a crazy way which I'm trying to manipulate/ change.

Please see the screenshot attached, the cell B2 is the raw data. There is normally information in columns C-F, which I've omitted for ease.

I want to create a macro which will open thefile then change the format of column B from the crazy date format it’s in to anormal date of d/m/yy hh:mm:ss – I can change the format manually by going intothe cell and changing it from to yy/mm/dd hh:mm however, this is stillincorrect and not what I want.

Any and allhelp greatly appreciated!


e_p

mmd089.jpg

 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this
- call as in example or use like an Excel formula in any cell
=StringToDateTime(B2)

In example below, C2 is formatted under Number Format \ Custom as dd/mm/yyyy hh:mm

Excel 2016 (Windows) 32 bit
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
B
[/th][th]
C
[/th][th]
D
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td]Raw data [/td][td]Converted[/td][td]Formula in C2[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td]19/04/2002 06:23[/td][td]
02/04/2019 06:23​
[/td][td] =StringToDateTime(B2)[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td][/td][td][/td][td][/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Sheet1[/td][/tr][/table]

Code:
Sub test()
    Dim aValue As String
    aValue = "19/04/2002 06:23"
    MsgBox Format(StringToDateTime(aValue), "dd/mm/yyyy hh:mm")
End Sub
Code:
Function StringToDateTime(ByVal aString As String) As Date
    Dim aTime As String, aDate As String
    Dim D As Integer, M As Integer, Y As Integer
    Dim hr As Integer, min As Integer
'get the time
    aTime = Split(aString, " ")(1)
    hr = Split(aTime, ":")(0)
    min = Split(aTime, ":")(1)
'get the date and switch day and year
    aDate = Split(aString, " ")(0)
    D = Split(aDate, "/")(2) - 2000
    M = Split(aDate, "/")(1)
    Y = Split(aDate, "/")(0) + 2000
'convert to correct date and time
    StringToDateTime = DateSerial(Y, M, D) + TimeSerial(hr, min, 0)
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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