に複数のテキストファイルのインポート2行目、私は2番目の行をインポートするマクロを記録している行2.エクセル
で始まるExcelにフォルダから複数のタブ区切りのテキストファイルの2行目インポートしたいです1行目のタブ区切りのテキストファイルを2行目からExcelに変換します(Excelのヘッダ行があります)。私はまた、複数のテキストファイルをExcelにインポートするコードを発見しました。私はそれらをまとめるのに困っている。私は記録されたマクロに複数のファイルインポート機能を追加したいと思います。
ここには、1つのテキストファイルの2番目の行をExcelの2番目の行にインポートするコードが記録されています。私はここで、複数のテキストファイルをインポートするためのコードがある
Sub Import()
'
' Import Macro
'
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\t830439\Desktop\test\BTWP004217_2017_6_29_12'14'03'0001.upl", _
Destination:=Range("$A$2"))
.CommandType = 0
.Name = "BTWP004217_2017_6_29_12'14'03'0001"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 2
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierNone
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
...その後3などを行、このコードは行2から始まるExcelに複数のテキストファイルの2行目インポートしたいです。上記の記録されたコードに追加された「すべてのファイルをループする」機能が必要です。私はすでにこのコードを実行するための正しい参照を追加しました。
Sub ReadFilesIntoActiveSheet()
Dim fso As FileSystemObject
Dim folder As folder
Dim file As file
Dim FileText As TextStream
Dim TextLine As String
Dim Items() As String
Dim i As Long
Dim cl As Range
' Get a FileSystem object
Set fso = New FileSystemObject
' get the directory you want
Set folder = fso.GetFolder("C:\Users\t830439\Desktop\test")
' set the starting point to write the data to
Set cl = ActiveSheet.Cells(2, 1)
' Loop thru all files in the folder
For Each file In folder.Files
' Open the file
Set FileText = file.OpenAsTextStream(ForReading)
' Read the file one line at a time
Do While Not FileText.AtEndOfStream
TextLine = FileText.ReadLine
' Parse the line into | delimited pieces
Items = Split(TextLine, "|")
' Put data on one row in active sheet
For i = 0 To UBound(Items)
cl.Offset(0, i).Value = Items(i)
Next
' Move to next row
Set cl = cl.Offset(1, 0)
Loop
' Clean up
FileText.Close
Next file
Set FileText = Nothing
Set file = Nothing
Set folder = Nothing
Set fso = Nothing
End Sub
ありがとうございました。
EDIT - UPDATE ------------------------------------------- -
私はコードをマージしましたが、ループに渡すことはできません。
Sub ReadFilesIntoActiveSheet3()
Dim fso As FileSystemObject
Dim folder As folder
Dim file As file
Dim FileText As TextStream
Dim TextLine As String
Dim Items() As String
Dim i As Long
Dim cl As Range
' Get a FileSystem object
Set fso = New FileSystemObject
' get the directory you want
Set folder = fso.GetFolder("C:\Users\t830439\Desktop\test")
' set the starting point to write the data to
Set cl = ActiveSheet.Cells(2, 1)
' Loop thru all files in the folder
For Each file In folder.Files
' Open the file
Set FileText = file.OpenAsTextStream(ForReading)
' Read the file one line at a time
Do While Not FileText.AtEndOfStream
TextLine = FileText.ReadLine
' Parse the line into | delimited pieces
'Items = Split(TextLine, "|")
' Put data on one row in active sheet
' For i = 0 To UBound(Items)
' cl.Offset(0, i).Value = Items(i)
' Next
'Move to next row
Set cl = cl.Offset(1, 0)
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\t830439\Desktop\test\BTWP004217_2017_6_29_12'14'03'0001.upl", _
Destination:=Range("$A$2"))
' .CommandType = 0
.Name = "BTWP004217_2017_6_29_12'14'03'0001"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 2
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierNone
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Loop
' Clean up
FileText.Close
Next file
Set FileText = Nothing
Set file = Nothing
Set folder = Nothing
Set fso = Nothing
End Sub
これが近くにあります。 私は助けが必要です: 1.タブ区切り文字としてテキストをインポートします 2.新しいシートではなくアクティブシートにインポートします。 – Regan
[OK]を、#2のために、この行を変更します。 セットBaseWksを= Workbooks.Add(xlWBATWorksheet).Worksheets(1) それはのようなものでなければなりません: セットBaseWks =ワークシート( "シート1") OR セットBaseWks = ActiveSheet 私はそう思っています...それが動作するか試してみることができますか? #1については...ハム...私はわからない。あなたがこのようなやり方をしているときにそれが問題だったことに気が付きませんでした。 – ryguy72
私はそれを試してみましょう、私は区切られたテキストに列Aを隠す別のマクロを作成しようとします。 – Regan