【发布时间】:2015-05-26 12:06:36
【问题描述】:
我正在使用 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 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
请有人告诉我一种做我需要的方法吗?提前致谢
【问题讨论】:
标签: vba excel text-files