How to import multiple txt files' data into one Excel sheet? - excel

I would like to get the data of many txt files in one folder into one Excel sheet.
I recorded a macro and ended up opening the files in different workbooks.
Sub GetTxtData()
ChDir "C:\Users\Desktop\TXT"
Workbooks.OpenText Filename:="C:\Users\Desktop\TXT\Data0.txt", Origin:= _
xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _
, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:= _
False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1) _
, Array(3, 1), Array(4, 1), Array(5, 1)), TrailingMinusNumbers:=True
Workbooks.OpenText Filename:="C:\Users\Desktop\TXT\Data1.txt", _
Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
TrailingMinusNumbers:=True
End Sub

Try importing the data instead of opening the csv files.
In Excel 2007 this is done by Data, From Text.
You'll get something like:
ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\Desktop\TXT\Data0.txt", Destination:=Range("$A$1")) ...
You're going to have to change the destination in code to keep the files from overwriting each other in the same spot. I'd use a full address including sheet name if I wanted them all on different sheets.

Related

I want to eliminate exact range (G1) & make this Text to column code dynamic to convert column values to date

ActiveCell.EntireColumn.Select
Selection.TextToColumns Destination:=Range("G1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 4), TrailingMinusNumbers:=True
Selection.NumberFormat = "dd-mmm-yy"
I found the solution , Thanks for support , Modified code should be
ActiveCell.EntireColumn.Select
Dim myRange As Range
Set myRange = Selection
myRange.TextToColumns Destination:=myRange, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 4), TrailingMinusNumbers:=True
Selection.NumberFormat = "dd-mmm-yy"

Issue converting UK date into text (returns US format)?

I have recorded a macro that will convert a column of dates in UK format to text by using Text to Columns when recording it, but the text it returns is in US format. Text to Columns does the job when its outside of a macro so not sure why it wouldn't work when used in a macro.
Is there a way to have the text in UK format and not have it converted to US format? I believe the below is the relevant part:
Selection.TextToColumns Destination:=Range("E1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 2), TrailingMinusNumbers:=True
Columns("F:F").Select
Selection.TextToColumns Destination:=Range("F1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 2), TrailingMinusNumbers:=True
Columns("A:P").Select
Selection.Copy
Windows("Xero Upload Template V2.xlsx").Activate
Range("A4").Select
ActiveSheet.Paste```
If I understand correctly then your data is in column 5 and 6 which you need to convert. Please try below code.
Dim c As Long
For c = 5 To 6
Columns(c).TextToColumns DataType:=xlDelimited, Tab:=False, Semicolon:=False,
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 5)
Columns(c).NumberFormat = "dd-mm-yyyy"
Next c

Macro for text-to-columns

I'm trying to write a code to do the following:
Using text-to-columns,the data should get divided in different columns.
The data in Cells A1-A8 is like this:
This data should appear in different columns.
Like this?
Sub Macro()
Selection.TextToColumns Destination:=Range("A1"), 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)),
End Sub
You can build the FieldInfo parameter dynamically by building out the string. In the example below, there are 10 resultant columns. The For loop builds the sFieldInfo variable, and specifies all the columns in Text format.
sComma = ""
sFieldInfo = ""
For x = 1 To 10
sFieldInfo = sFieldInfo & sComma & " Array(" & x & ", xlTextFormat)"
sComma = ","
Next x
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.TextToColumns Destination:=Range("J5"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar:="/", _
TrailingMinusNumbers:=True, _
FieldInfo:=Array(sFieldInfo)

Convert to number error in excel after copy paste

I am facing issue after copy and paste from export file from SAP it ask to convert to number option which is like error without converting to number formulas do not work. also cell value is number only.
I tried to make macro but it works on single column only. how to make it work on multiple column together and make it faster also as it stuck exel for long time.
code for converting to number
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Columns("F:F").Select
Selection.TextToColumns Destination:=Range("F1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Columns("G:G").Select
Selection.TextToColumns Destination:=Range("G1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
How shorter code and combine for multiple columns in single line.
Use an xlFixedWidth in your Range.TextToColumns method.
Dim c As Long, vCOLs As Variant
vCOLs = Array(1, 6, 7) 'columns A, F and G
With Worksheets("Sheet1")
For c = LBound(vCOLs) To UBound(vCOLs)
With .Column(c)
.TextToColumns Destination:=.Cells(1), DataType:=xlFixedWidth, _
FieldInfo:=Array(0, 1), TrailingMinusNumbers:=True
End With
Next c
End With
The variant array allow you to quickly specify the columns to be processed. Even with a large (~250K) number of rows, this should be fairly quick to cycle through.

AHK_L Com Excel TextToColumns

I need some help getting my script to work with AHK_L COM Excel on TextToColumns. I've looked around but can't seem to locate enough examples to get me going. When I have run my VBA macro this is what I end up with:
Selection.TextToColumns Destination:=Range("D1"), 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)), _
TrailingMinusNumbers:=True
Examples I have found look like my code should be:
xl.Selection.TextToColumns([color=#FF0000]xl.range("D1")[/color],1,1,0,1,0,0,0,1,":")
But nothing happens to my column that's selected.
Any ideas? Thanks in advance for your time.
xl.Selection.TextToColumns(xl.range("D1"),1,1,0,1,0,0,0,1,":")
Try it with out the color tags

Resources