Reputation: 1886
I'm having an issue with Excel VBA and the Text-to_Columns.
I have 30+ text files, some containing dates in the format DD/MM/YYYY. I'm using a VBA macro to import them, remove data that is not needed, and then recombining the files into a CSV format. The smallest files contain about 600 rows and 20 columns, the largest contain 1,000,000 rows and 93 columns (800mb+ file).
When I extract them using VBA text to columns codes, the formats of the dates changes from DD/MM/YYYY to MM/DD/YYYY. (e.g. 05/11/2015 - 5th November, 2015 becomes 11/05/2015, 11th May, 2015). The dates are not normally in the same column, and never have the same name in each file.
This is the VBA code used:
Sub Stripper()
' Go through each spreadsheet, and extract from Text-to-column
' System allows for 150 columns
' Text-to-column can handle commas, semicolons, and pipe delimitations
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.DisplayStatusBar = False
Dim y As Variant
Dim lastColumn As Integer
Dim k As Integer
Dim MT As Integer
Dim Cd As Integer
k = Sheets.Count
y = 1
MT = 0
Cd = 0
While y <= k
t = Sheets(y).Name
If t = "Launch" Then
ElseIf Left(t, 4) = "MFGI" Or IsNumeric(Left(t, 1)) = True Then
Worksheets(t).Activate
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=True, OtherChar _
:="|", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), _
Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), _
Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array(16, 1), Array(17, 1), Array(18, 1), Array(19, 1), Array(20, 1), _
Array(21, 1), Array(22, 1), Array(23, 1), Array(24, 1), Array(25, 1), Array(26, 1), Array(27, 1), Array(28, 1), Array(29, 1), Array(30, 1), _
Array(31, 1), Array(32, 1), Array(33, 1), Array(34, 1), Array(35, 1), Array(36, 1), Array(37, 1), Array(38, 1), Array(39, 1), Array(40, 1), _
Array(41, 1), Array(42, 1), Array(43, 1), Array(44, 1), Array(45, 1), Array(46, 1), Array(47, 1), Array(48, 1), Array(49, 1), Array(50, 1), _
Array(51, 1), Array(52, 1), Array(53, 1), Array(54, 1), Array(55, 1), Array(56, 1), Array(57, 1), Array(58, 1), Array(59, 1), Array(60, 1), _
Array(61, 1), Array(62, 1), Array(63, 1), Array(64, 1), Array(65, 1), Array(66, 1), Array(67, 1), Array(68, 1), Array(69, 1), Array(70, 1), _
Array(71, 1), Array(72, 1), Array(73, 1), Array(74, 1), Array(75, 1), Array(76, 1), Array(77, 1), Array(78, 1), Array(79, 1), Array(80, 1), _
Array(81, 1), Array(82, 1), Array(83, 1), Array(84, 1), Array(85, 1), Array(86, 1), Array(87, 1), Array(88, 1), Array(89, 1), Array(90, 1), _
Array(91, 1), Array(92, 1), Array(93, 1), Array(94, 1), Array(95, 1), Array(96, 1), Array(97, 1), Array(98, 1), Array(99, 1), Array(100, 1), _
Array(101, 1), Array(102, 1), Array(103, 1), Array(104, 1), Array(105, 1), Array(106, 1), Array(107, 1), Array(108, 1), Array(109, 1), Array(110, 1), _
Array(111, 1), Array(112, 1), Array(113, 1), Array(114, 1), Array(115, 1), Array(116, 1), Array(117, 1), Array(118, 1), Array(119, 1), Array(120, 1), _
Array(121, 1), Array(122, 1), Array(123, 1), Array(124, 1), Array(125, 1), Array(126, 1), Array(127, 1), Array(128, 1), Array(129, 1), Array(130, 1), _
Array(131, 1), Array(132, 1), Array(133, 1), Array(134, 1), Array(135, 1), Array(136, 1), Array(137, 1), Array(138, 1), Array(139, 1), Array(140, 1), _
Array(141, 1), Array(142, 1), Array(143, 1), Array(144, 1), Array(145, 1), Array(146, 1), Array(147, 1), Array(148, 1), Array(149, 1), Array(150, 1)), _
TrailingMinusNumbers:=True
s = 1
lastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
While s <= lastColumn
Cells(1, s).EntireColumn.Select
If Cells(1, s) = "Original Data Source" Or Cells(1, s) = "Original Data Source Table/Field" Then
Selection.Delete
lastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
Else
s = s + 1
End If
Wend
Range(Cells(1, s), Cells(1, s + 30)).EntireColumn.Select
Selection.Delete
MT = MT + 1
End If
y = y + 1
Wend
Worksheets("Launch").Activate
Range("D28") = y - 2
Range("F28") = MT
Range("D31") = Cd
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.DisplayStatusBar = True
End Sub
Is there any way to stop VBA changing the dates around as it is throwing out all the reconciliation on the files?
Upvotes: 1
Views: 3245
Reputation: 21
Actually it is quite simple although it took me a while to find the answer. in "Array(6, 1)" the last "1" is the format of the cell. 1 means general, that is Excel tries to determine what the cell contains, text, date, numbers. If you are sure the cell contains a date, change that value to 4 in order to format the cell as date DD/MM/YY. Array(6, 4). See this article from Microsoft for all the values: XlColumnDataType Enumeration (Excel) This is a lick to the complete article Range.TextToColumns Method (Excel) I hope this helps you guys along
Upvotes: 2