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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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
B
C
D
1
Raw data ConvertedFormula in C2
2
19/04/2002 06:23
02/04/2019 06:23​
=StringToDateTime(B2)
3
Sheet: Sheet1

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,213,484
Messages
6,113,923
Members
448,533
Latest member
thietbibeboiwasaco

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