Excel vba:导入多个文本文件,并在导入后移动文件?
我真的希望有人能帮上忙。目前,我正在使用vba将文本文件中的每一行文本导入到一行的新列中。每次我运行这个函数时,都会在前一行的下面创建一行新的数据 结果:Excel vba:导入多个文本文件,并在导入后移动文件?,excel,text,import,Excel,Text,Import,我真的希望有人能帮上忙。目前,我正在使用vba将文本文件中的每一行文本导入到一行的新列中。每次我运行这个函数时,都会在前一行的下面创建一行新的数据 结果: Row 1 (Showing Data from TextFile 1) Column A Column B Column C Data Data Data Row 2 (Showing Data from TextFile 2) Column A Column
Row 1 (Showing Data from TextFile 1)
Column A Column B Column C
Data Data Data
Row 2 (Showing Data from TextFile 2)
Column A Column B Column C
Data Data Data
所以这一切都很好,在我从文件中导入文本之后,文件将从我的目录“unAction”移动到一个名为“actioned”的目录中
因此,目前我的代码还不太清楚,我目前必须定义文本文件名,以便我可以将数据从文本文件导入到我的电子表格中,并且再次定义我要移动的文本文件名,此代码当前仅适用于1个文本文件。但是,我希望能够做到的是,如果我的文件夹“未动作”中有多个文本文件,那么我希望将这些文本文件中的每一个导入到一个新行中,并同时将刚导入数据的所有文本文件移动到我的文件夹“动作”中
这是我的密码:
Sub ImportFile()
Dim rowCount As Long
rowCount = ActiveSheet.UsedRange.Rows.Count + 1
If Cells(1, 1).Value = "" Then rowCount = 1
Close #1
Open "Y:\Incident Logs\Unactioned\INSC89JH.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 = "Y:\Incident Logs\Unactioned\"
destPath = "Y:\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
子导入文件()
暗行数与长行数相同
rowCount=ActiveSheet.UsedRange.Rows.Count+1
如果单元格(1,1).Value=”“,则行计数=1
关闭#1
打开“Y:\Incident Logs\unAction\INSC89JH.txt”作为#1输入
A=1
不执行时执行EOF(1)
行输入#1,文本行
单元格(行数,A)=文本行
A=A+1
环
关闭#1
尺寸d作为字符串,外部,x
Dim srcPath作为字符串,destPath作为字符串,srcFile作为字符串
srcPath=“Y:\Incident Logs\unaction\”
destPath=“Y:\Incident Logs\actions\”
ext=数组(“*.txt”,“*.xls”)
对于ext中的每个x
d=Dir(srcPath&x)
在d“”时执行
srcFile=srcPath&d
FileCopy srcFile,destPath&d
杀死srcFile
d=Dir
环
下一个
端接头
请有人告诉我,我将如何修改此代码来做我需要它做的事情?提前感谢我建议将代码分解为多个函数 您可以将ImportFile方法更改为不杀死所有文件,只杀死它所操作的文件,然后让它一次对一个特定文件进行操作。例如:
Sub ImportFile(directory As String, filename As String)
Dim rowCount As Long
rowCount = ActiveSheet.UsedRange.Rows.Count + 1
If Cells(1, 1).Value = "" Then rowCount = 1
Close #1
Open directory & filename 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
'Move the file and delete it
Dim srcPath As String, destPath As String
srcPath = directory & filename
destPath = "C:\Incident Logs\Actioned\" & filename
FileCopy srcPath, destPath
Kill srcPath
End Sub
然后,这里是另一个stackoverflow帖子
因此,只要稍作调整,您就可以得到如下结果:
Sub ImportAllFiles()
ImportFilesWithExtension "*.txt"
ImportFilesWithExtension "*.xls*"
End Sub
Sub ImportFilesWithExtension(extension As String)
Dim StrFile As String, myDir As String
myDir = "C:\Incident Logs\Unactioned\"
StrFile = Dir(myDir & extension)
Do While Len(StrFile) > 0
ImportFile myDir, StrFile
StrFile = Dir
Loop
End Sub
我还将其分解为函数:
Sub ImportFile()
Dim rLastCell As Range
Dim vFolder As Variant
Dim vFile As Variant
Dim colFiles As Collection
With ThisWorkbook.Worksheets("Sheet1") 'Note - update sheet name.
'First find the last cell on the named sheet.
Set rLastCell = .Cells.Find( _
What:="*", _
LookIn:=xlValues, _
SearchDirection:=xlPrevious)
If rLastCell Is Nothing Then
'Set LastCell to A2.
Set rLastCell = .Cells(2, 1)
Else
'Set LastCell to column A, last row + 1
Set rLastCell = .Range(rLastCell.Row + 1, 1)
End If
vFolder = GetFolder()
Set colFiles = New Collection
EnumerateFiles vFolder, "\*.txt", colFiles
For Each vFile In colFiles
'Do stuff with the file.
'Close the file and move it.
MoveFile CStr(vFile), "S:\Bartrup-CookD\Text 1\" & Mid(vFile, InStrRev(vFile, "\") + 1, Len(vFile)) 'Note - update folder name.
Next vFile
End With
End Sub
这将把所有文件放入一个集合:
Sub EnumerateFiles(ByVal sDirectory As String, _
ByVal sFileSpec As String, _
ByRef cCollection As Collection)
Dim sTemp As String
sTemp = Dir$(sDirectory & sFileSpec)
Do While Len(sTemp) > 0
cCollection.Add sDirectory & "\" & sTemp
sTemp = Dir$
Loop
End Sub
这将要求您选择一个文件夹:
' To Use : vFolder = GetFolder()
' : vFolder = GetFolder("S:\Bartrup-CookD\Customer Services Phone Reports")
Function GetFolder(Optional startFolder As Variant = -1) As Variant
Dim fldr As FileDialog
Dim vItem As Variant
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
If startFolder = -1 Then
.InitialFileName = Application.DefaultFilePath
Else
If Right(startFolder, 1) <> "\" Then
.InitialFileName = startFolder & "\"
Else
.InitialFileName = startFolder
End If
End If
If .Show <> -1 Then GoTo NextCode
vItem = .SelectedItems(1)
End With
NextCode:
GetFolder = vItem
Set fldr = Nothing
End Function
'----------------------------------------------------------------------
' MoveFile
'
' Moves the file from FromFile to ToFile.
' Returns True if it was successful.
'----------------------------------------------------------------------
Public Function MoveFile(FromFile As String, ToFile As String) As Boolean
Dim objFSO As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.MoveFile FromFile, ToFile
MoveFile = (Err.Number = 0)
Err.Clear
End Function