Excel VBA:是否将目录中所有文本文件中的数据导入Excel中的新行?
我正在使用VBA将单个文本文件中的所有数据导入excel中的新行,然后在导入文本文件中的数据后,将文本文件移动到另一个位置/目录。我做了这么多 但是,现在我在一个目录中有多个文本文件,希望从每个文本文件中获取数据并将其插入新行,因此我最终得到以下结果:Excel VBA:是否将目录中所有文本文件中的数据导入Excel中的新行?,vba,excel,text-files,Vba,Excel,Text Files,我正在使用VBA将单个文本文件中的所有数据导入excel中的新行,然后在导入文本文件中的数据后,将文本文件移动到另一个位置/目录。我做了这么多 但是,现在我在一个目录中有多个文本文件,希望从每个文本文件中获取数据并将其插入新行,因此我最终得到以下结果: Text File 1 A B C D Text File 2 T L V P Excel: Column A Column B Column C Column D A
Text File 1
A
B
C
D
Text File 2
T
L
V
P
Excel:
Column A Column B Column C Column D
A B C D
T L V P
这是我的密码:
Sub ImportFile()
Dim rowCount As Long
rowCount = ActiveSheet.UsedRange.Rows.Count + 1
If Cells(1, 1).Value = "" Then rowCount = 1
Close #1
Open "Z:\Incident Logs\Unactioned\IN94LQ3Z.txt" For Input As #1
A = 1
Do While Not EOF(1)
Line Input #1, TextLine
Cells(rowCount, A) = TextLine
A = A + 1
Loop
Close #1
Dim d As String, ext, x
Dim srcPath As String, destPath As String, srcFile As String
srcPath = "Z:\Incident Logs\Unactioned\"
destPath = "Z:\Incident Logs\Actioned\"
ext = Array("*.txt", "*.xls")
For Each x In ext
d = Dir(srcPath & x)
Do While d <> ""
srcFile = srcPath & d
FileCopy srcFile, destPath & d
Kill srcFile
d = Dir
Loop
Next
End Sub
有人能告诉我怎么做吗?提前感谢您需要将以下代码片段与您的代码合并,以替换硬编码的文件路径和文件名。此外,您还需要确保在打开新文件时索引正确
Sub AllFilesInFolder()
Dim myFolder As String, myFile As String
myFolder = Application.FileDialog(msoFileDialogFolderPicker)
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then
myFolder = .SelectedItems(1)
End If
End With
myFile = Dir(myFolder & "\*.txt") '
Do While myFile <> ""
Open myFolder & "\" & myFile For Input As #1
.....
myFile = Dir
Loop
End Sub
很抱歉,我不明白你的答案,你能告诉我你的代码如何与我的代码一起工作吗?谢谢