How to format date when converting text to columns? - excel

I'm trying to convert a range of data, from text to columns, with VBA.
When I apply the code some cells with date don't convert as date. If then you enter the cell and press "enter" it converts to date format.
Selection.TextToColumns _
Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=True, _
Semicolon:=False, _
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, 4), Array(7, 8), Array(8, 4), Array(9, 4), Array(10, 4), _
Array(11, 4), Array(12, 4), 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)),
DecimalSeparator:=".", _
ThousandsSeparator:=",", _
TrailingMinusNumbers:=True
In the array, cells 6 to 12 is where the dates are.

I cannot update any of the data due to data protection policy. But here is a small sample where also happens the same problem. All the text is in the same column A1 and each line in a different row
20191284809$$BT$Anulado$23/05/2019 09:22:57
20191304047$$BT$Anulado$04/06/2019 20:29:03
20191159599$$BT$Anulado$01/03/2019 11:53:10
20191165919$$BT$Anulado$06/03/2019 11:33:18
20191247883$$BT$Anulado$27/04/2019 14:54:17
20191381935$$BT$Cerrado$22/07/2019 01:12:03
20191404309$$BT$Cerrado$03/08/2019 08:56:55

Related

Run Complicated Macro on Many Files

I have a large list of .txt files that I need to have a macro that does the following:
Open Files
Delimit the file based on "|"
Select all then filter
Sort on a specific header
Steps 3 and 4 are easy... If these files weren't all .txt with | delimiters, I know how to open multiple files and then filter/sort, the issue I run into is step 2.
Code so far:
Option Explicit
Dim theDir As String, wk As Workbook, numFiles As Integer, s As String, r As Range
Const ext = ".txt"
Sub LoopThroughFiles()
Dim xFd As FileDialog
Dim xFdItem As Variant
Dim xFileName As String
theDir = ThisWorkbook.Path
s = Dir(theDir & "\*" & ext)
Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
If xFd.Show = -1 Then
xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
xFileName = Dir(xFdItem & "*.txt*")
Do While xFileName <> ""
With Workbooks.Open(xFdItem & xFileName)
'your code here
Set r = Range(Range("A1"), Range("A1").End(xlDown))
r.TextToColumns Destination:=r, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, 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)), TrailingMinusNumbers:=True
Application.DisplayAlerts = False
s = Dir()
numFiles = numFiles + 1
xFileName = Dir
End With
Loop
End If
End Sub
This code works... but only for the first column, I have upwards of 70 columns in some documents.
You could use the Workbooks.OpenText method - a bit easier to manage I think
Sub Tester()
Dim wb As Workbook
Set wb = GetWorkbook("C:\Temp\pipes.txt")
Debug.Print wb.Name
End Sub
Function GetWorkbook(fpath) As Workbook
Workbooks.OpenText Filename:=fpath, Origin:=437, StartRow:= _
1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=False _
, Space:=False, Other:=True, OtherChar:="|", TrailingMinusNumbers:=True
Set GetWorkbook = ActiveWorkbook
End Function
You are selecting the first column in this line of code.
Set r = Range(Range("A1"), Range("A1").End(xlDown))
This should be OK if the files are text delimited by the pipe symbol.
However if there are commas in the files, it will automatically break data after the comma into another column.
Try opening the files directly in text mode.
As an example
Workbooks.OpenText Filename:="C:\Temp\Test1.txt", _
Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
, Comma:=False, Space:=False, Other:=True, OtherChar:="|", FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1)), _
TrailingMinusNumbers:=True
I got this to work:
Option Explicit
Dim theDir As String, wk As Workbook, numFiles As Integer, s As String, r As Range
Const ext = ".txt"
Sub LoopThroughFiles()
Dim xFd As FileDialog
Dim xFdItem As Variant
Dim xFileName As String
theDir = ThisWorkbook.Path
Dim wkbpath As String
Dim wkbname As String
Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
If xFd.Show = -1 Then
xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
xFileName = Dir(xFdItem) ' old version had: & "*.txt*")
Do While xFileName <> ""
With Workbooks.Open(xFdItem & xFileName)
'your code here
Set r = Range(Range("A1"), Range("A1").End(xlDown))
r.TextToColumns Destination:=r, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, 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)), TrailingMinusNumbers:=True
Application.DisplayAlerts = False
Cells.Select
Selection.AutoFilter
Application.AddCustomList ListArray:=Array("PREFERRED", "NON-PREFERRED", _
"UNACCEPTABLE", "OBSOLETE")
ActiveSheet.Sort.SortFields. _
Clear
ActiveSheet.Sort.SortFields. _
Add Key:=Range("D2:D479"), SortOn:=xlSortOnValues, _
CustomOrder:="PREFERRED,NON-PREFERRED,UNACCEPTABLE,OBSOLETE", DataOption:= _
xlSortNormal
With ActiveSheet.Sort
.SetRange Range("A1:BH79")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
xFileName = Dir
wkbpath = "C:\Users\tomas.breitinger\Desktop\BAE Export .DAT Files\Finished\"
wkbname = ActiveWorkbook.Name
ActiveWorkbook.SaveAs Filename:= _
wkbpath & wkbname & ".xlsx", FileFormat:=51, CreateBackup:=False
ActiveWorkbook.Close savechanges:=False
End With
End With
Loop
End If
End Sub

Need help using Text to Columns & Fieldinfo to parse columns with VBA in Excel

Per comments below, I'm editing my original posted question to more accurately reflect my problem.
This is for my job. In my work program, I download my data table to a csv file and it automatically opens the data in Notepad. I don't save the file in Notepad. (This process has to remain the same per my job. Saving the data is unnecessary and would take too much time as I have to do this same step hundreds of times a day.)
Once the data opens in Notepad, I copy all the data. I then need to paste the data directly into excel. The data is comprised of 66 different columns and includes headers. I need to parse the columns so that many of them are skipped, while most of the others are pasted as text. In the array below, i thought i laid that out correctly using the Array(1, 9), Array(4, 2) method, hoping Column 1 would be skipped and Column 4 would be pasted as text, etc...
When running this code though, the first column is skipped, but then all subsequent columns are pasted, not following any of the array setup i wrote in the code. I've researched questions on this site and many others. I can't figure what is wrong with my code to cause this issue.
Here is a link to the data I'm using (edited for anonymity of data):
https://www.dropbox.com/s/2ujbpgm3zkvserx/Test_Data_Parsing.txt?dl=0
Here is the code I'm using:
Sub Test_PasteParsing()
With ThisWorkbook.Sheets(1)
Range("A1").PasteSpecial
End With
Columns("A:A").TextToColumns _
Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=True, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=False, _
OtherChar:=False, _
FieldInfo:=Array(Array(1, 9), Array(2, 9), Array(3, 4), Array(4, 2), Array(5, 9), Array(6, 9), _
Array(7, 9), Array(8, 2), Array(9, 9), Array(10, 9), Array(11, 2), Array(12, 9), _
Array(13, 2), Array(14, 9), Array(15, 9), Array(16, 2), Array(17, 9), Array(18, 9), _
Array(19, 9), Array(20, 9), Array(21, 9), Array(22, 9), Array(23, 9), Array(24, 9), _
Array(25, 9), Array(26, 9), Array(27, 9), Array(28, 2), Array(29, 2), Array(30, 2), _
Array(31, 2), Array(32, 9), Array(33, 9), Array(34, 9), Array(35, 9), Array(36, 9), _
Array(37, 9), Array(38, 2), Array(39, 9), Array(40, 2), Array(41, 9), Array(42, 9), _
Array(43, 9), Array(44, 9), Array(45, 9), Array(46, 9), Array(47, 9), Array(48, 9), _
Array(49, 9), Array(50, 9), Array(51, 2), Array(52, 2), Array(53, 2), Array(54, 2), _
Array(55, 9), Array(56, 9), Array(57, 2), Array(58, 9), Array(59, 2), Array(60, 9), _
Array(61, 9), Array(62, 2), Array(63, 9), Array(64, 9), Array(65, 2), Array(66, 2)), _
TrailingMinusNumbers:=True
End Sub

how to get field width info of the excel text to column

While converting text to column using excel wizard. how to get the field info array used in this conversion. for example in the vba code generated in a sample text to column, how can we procure the fieldinfo array used
Array(0, 1), Array(24, 1), Array(44, 1) etc. preferably in a variable
Selection.TextToColumns Destination:=Range("A5"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(24, 1), Array(44, 1), Array(70, 1), Array(79, 1), _
Array(96, 1), Array(107, 1), Array(118, 1), Array(127, 1), Array(135, 1)), _
TrailingMinusNumbers:=True
Like this?
Dim fldInfo
fldInfo = Array(Array(0, 1), Array(24, 1), Array(44, 1), Array(70, 1), Array(79, 1), _
Array(96, 1), Array(107, 1), Array(118, 1), Array(127, 1), Array(135, 1))
Selection.TextToColumns Destination:=Range("A5"), DataType:=xlFixedWidth, _
FieldInfo:=fldInfo, TrailingMinusNumbers:=True

Modify recorded code to convert Text to Column

I have a column BA. I want to do Text to Column to column BC.
I recorded a macro. When I execute it in a workbook I get an error.
Here is my recorded macro.
Sub Macro8()
'
' Macro8 Macro
'
' Keyboard Shortcut: Ctrl+Shift+H
'
Selection.TextToColumns Destination:=ActiveCell.Offset(0, 2).Range("A1"), _
DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter _
:=True, Tab:=False, Semicolon:=False, Comma:=True, 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)), TrailingMinusNumbers:=True
End Sub
Shouldn't Range be BA?
I don't see BC - is that the 0, 2 offset?
EDIT: changed the destination offset to two columns over (from 1)
If you just need your code to operate on the selected cells then this should work:
Selection.TextToColumns Destination:=Selection.Offset(0, 2), _
DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Comma:=True, Other:=True, _
OtherChar:=":"
Unless you have specific datatypes etc you want to apply to the resulting columns, you can typically omit the FieldInfo argument

Dynamic Destination VBA

I'm trying to make my VBA destination to the cell below the ActiveCell. Right now, it only wants to place it in R2. I have tried using offset to shift it down a cell, but haven't had any luck. The array would typically hold something like Doe, John JDoe11#email.com.
Sub MailMergeNames()
'
' MailMergeNames Macro
'
' Keyboard Shortcut: Ctrl+Shift+M
'
'
' Shift line to rows
'
Selection.TextToColumns Destination:=Range("R1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, 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)), TrailingMinusNumbers:=True
Range("R1:CJ1").Select
Selection.Copy
Range("R2").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Application.CutCopyMode = False
'
' Trim Values
'
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim Cell As Range
'Also Treat CHR 0160, as a space (CHR 032)
Selection.Replace what:=Chr(160), Replacement:=Chr(32), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
'Trim in Excel removes extra internal spaces, VBA does not
On Error Resume Next 'in case no text cells in selection
For Each Cell In Intersect(Selection, _
Selection.SpecialCells(xlConstants, xlTextValues))
Cell.Value = Application.Trim(Cell.Value)
Next Cell
On Error GoTo 0
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
'
' Deliminate Rows to Columns
'
Selection.TextToColumns Destination:=Range("R2"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=True, Comma:=True, Space:=True, Other:=True, OtherChar:= _
"<", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1)) _
, TrailingMinusNumbers:=True
Range("R1", "AAA1").Clear
Range("R2").Select
End Sub

Resources