VBA causing time values to go to AM/PM

eddm87

New Member
Joined
Jan 20, 2023
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Using imported data, I need to activate text to column on a column to be able to use the data, sum etc. This is column B in the below shot.
1681611698383.png


This works fine when manually doing it however I am trying to create a VBA code which does various things, put data in table, delimits column A etc.

The code is...

Columns("B:B").Select
Selection.TextToColumns Destination:=Range("B1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(0, 1), TrailingMinusNumbers:=True
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
Range("B1").Select
ActiveCell.FormulaR1C1 = "Time"
Selection.End(xlToLeft).Select
Selection.End(xlUp).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A:$S"), , xlYes).Name = _
"Table1"
Columns("A:S").Select
ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium6"
Selection.End(xlUp).Select
Selection.End(xlToLeft).Select

The below is what I end up with (column C is the one in question)
1681611873804.png


It doesn't matter if it is fixed width or delimited it always reverts to PM or AM. I have changed the format to [h]:mm:ss before VBA, after VBA, within the code etc, doesn't make a difference.
Any help would be great.

Thanks all.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
To anyone interested. Spent another couple of hours on it and found that column A was delimiting two other values, one a time, one a PM/AM, I just couldn't see it. I created an additional column, then deleted it.
Updated VBA below.

Columns("B:B").Select
Range("B:B").Value = Range("B:B").Value
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A:A"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
Columns("C:C").Delete
Columns("C:C").Select
Selection.NumberFormat = "[h]:mm:ss"
Selection.End(xlToLeft).Select
Selection.End(xlUp).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A:$S"), , xlYes).Name = _
"Table1"
Columns("A:S").Select
ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium6"
Range("Table1[[#Headers],[Column1]]").Select
ActiveCell.FormulaR1C1 = "Time"
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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