I have been lurking for a while and trying to find a solution, but to no avail.
So the problem is that i have a column with the following data format of date and time. It's custom and exported from some hardware.
27.08.2024 08:20:19
(DD.MM.YYYY hh:mm:ss) (thank the commenter for noticing that this was not in code)
Excel doesn't recognise this type of formatting, therefore on chart's x-axis it keeps it intact as is. Also, Excel does recognise this as Text i suppose, because when converting to Number nothing happens (actually nothing happens even with any other format).
What i noticed: manually deleting the spacebar between date and time, and then typing a spacebar back makes it digestible for formatting. In fact all the formats work properly from that point on. (by keys: select cell -> F2 -> Backspace the Space -> Type the space -> Enter = formatting works...)
step 1->27.08.2024 08:19:59
step 2->27.08.202408:19:59
step 3->27.08.2024 08:19:59
TL:DR Here. Problem is that there are up to 10th of thousands of rows with weird format, that excel understands only as text. Manually deleting spacebar and then typing it back does the magic, but this is not the option. I tested a VBA. End goal: to make a chart show only time without the date, while leaving the data intact (without deleting the date from the cells or using a delimiter (this can change if there is no other option)).
Sub test()
Dim nLastRow, i As Integer
Dim wbCurrent As Workbook
Dim wsCurrent As Worksheet
Set wbCurrent = ActiveWorkbook
Set wsCurrent = wbCurrent.ActiveSheet
i = 1
'the Last row index
nLastRow = Cells(Rows.Count, i).End(xlUp).Row
'putting a space after date, because that somehow fixes the stupid formatting.
For i = 1 To nLastRow Step 1
If InStr(1, wsCurrent.Cells(i, 1).Value, " ", vbTextCompare) > 0 Then
wsCurrent.Cells(i, 1) = Replace(Cells(i, 1).Value, " ", " ", 1, 1)
End If
Next i
End Sub
It does the job of replacing the spacebar with a spacebar, but doesn't have the same effect as with manual. Maybe i am missing something like instead of vbTextCompare it should do vbBinaryCompare.
No difference between MacOS and Windows.
Thanks.
UPD. Resolved.
It required some fiddling with the local time and writing standards. So the formula in excel looks like this now. instead of , it uses ; instead of DD/MM/YYYY it is German TT/MM/JJJJ
=DATEVALUE(TEXT(SUBSTITUTE(LEFT(A2;10);".";"/");"TT/MM/JJJJ")) + TIMEVALUE(RIGHT(A2;8))
Now this is the part of code that i implemented.
With ActiveSheet
'[-Block-Start-----Block to separate Date and Time from the Column 1--------]
RowIndex = 1
LastRowIndex = Cells(Rows.Count, RowIndex).End(xlUp).Row
Range("B2").EntireColumn.Insert Shift:=xlToRight
Range("B1").Value = "Time"
RefCellAddress = .Cells(2, 1).Address(False, False, xlR1C1, False, RelativeTo:=Cells(2, 2))
.Cells(2, 2).Formula = "=DATEVALUE(TEXT(SUBSTITUTE(LEFT(" & RefCellAddress & ",10),""."",""/""),""TT/MM/JJJJ"")) + TIMEVALUE(RIGHT(" & RefCellAddress & ",8))"
'the original formula =DATEVALUE(TEXT(SUBSTITUTE(LEFT(A2;10);".";"/");"DD/MM/YYYY")) + TIMEVALUE(RIGHT(A2;8))
'Spread the formula to the whole column
Set FormulaCell = .Cells(2, 2)
.Range(Cells(LastRowIndex, FormulaCell.column), Cells(FormulaCell.Row, FormulaCell.column)).FillDown
'[-Block-End-------------]
'rest of code there...
It's ugly but... hey, it works! Thanks everyone!