【发布时间】:2015-05-27 09:40:02
【问题描述】:
我正在使用以下 vba 代码将我的所有文本文件导入到 excel 中的新行中。这一点工作正常,接下来我要做的是一旦导入了文本文件,我希望将每个文本文件从一个目录 'Z:\NS\Unactioned\' 移动到另一个名为 Actioned 'Z:\NS\Actioned\&Filename\' 的目录。
然后在该文件夹中,根据文件名(减去文件扩展名)为每个文本文件创建一个文件夹,然后我可以将每个文本文件放在相应的文件夹中。
所以如果我的文件夹中有 3 个 .txt 文件 Unactioned:
1.txt
2.txt
3.txt
然后每个 txt 文件将像这样移动:
Actioned/1/1.txt
Actioned/2/2.txt
Actioned/3/3.txt
有人可以告诉我如何做到这一点吗?谢谢
代码:
Sub Import_All_Text_Files_2007()
Dim nxt_row As Long
'Change Path
Const strPath As String = "Z:\NS\Unactioned\"
Dim strExtension As String
'Stop Screen Flickering
Application.ScreenUpdating = False
ChDir strPath
'Change extension
strExtension = Dir(strPath & "*.txt")
Do While strExtension <> ""
'Sets Row Number for Data to Begin
If Range("C1").Value = "" Then
nxt_row = 1
Else
If Range("C2").Value = "" Then
nxt_row = 2
Else
nxt_row = Range("C1").End(xlDown).Offset(1).Row
End If
End If
'Below is from a recorded macro importing a text file
FileNum = FreeFile()
curCol = 3
Open strPath & strExtension For Input As #FileNum
While Not EOF(FileNum)
Line Input #FileNum, DataLine
ActiveSheet.Cells(nxt_row, curCol) = DataLine
curCol = curCol + 1
Wend
Close #FileNum
strExtension = Dir
Loop
Dim d As String, ext, x
Dim srcPath As String, destPath As String, srcFile As String
srcPath = "Z:\NS\Unactioned\"
destPath = "Z:\NS\Actioned\" & srcFile & "\"
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
Application.ScreenUpdating = True
End Sub
【问题讨论】: