vba - 将工作簿一一复制到存档文件夹
问题描述
我使用了下面的代码,它完美地完成了拾取所有文件并将它们移动到存档文件夹的工作。但是,我正在寻找要逐个提取的文件,将日期放入目标工作簿并将其移动到存档文件夹,此过程一直持续到最后一个工作簿。我使用下面的代码来复制数据并调用 Sub 例程来执行存档。但它会在第一次迭代中将所有文件一次移动到存档文件夹中。
Sub Test()
Dim xWb As Workbook
Dim xToBook As Workbook
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Dim xFiles As New Collection
Dim I As Long
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder [Kutools for Excel]"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
If Right(xStrPath, 1) <> "\" Then xStrPath = xStrPath & "\"
xFile = Dir(xStrPath & "*.xlsx")
If xFile = "" Then
MsgBox "No files found", vbInformation, "Kutools for Excel"
Exit Sub
End If
Do While xFile <> ""
xFiles.Add xFile, xFile
xFile = Dir()
Loop
Set xToBook = ThisWorkbook
If xFiles.Count > 0 Then
For I = 1 To xFiles.Count
Set xWb = Workbooks.Open(xStrPath & xFiles.Item(I))
xWb.Worksheets(1).Copy after:=xToBook.Sheets(xToBook.Sheets.Count)
On Error Resume Next
ActiveSheet.Name = xWb.Name
On Error GoTo 0
xWb.Close False
'**Call MoveFiles_3**
Next I
End If
End Sub
Sub MoveFiles_3()
Dim fso As Object, d As String, ext, x
Dim srcPath As String, destPath As String, srcFile As String
srcPath = "C:\Users\userfolder\Desktop\Test Macro\"
destPath = "C:\Users\userfolder\Desktop\Archive Test\"
Set fso = CreateObject("Scripting.FileSystemObject")
ext = Array("*.xlsx")
MsgBox Dir(srcPath)
For Each x In ext
d = Dir(srcPath & x)
Do While d <> ""
srcFile = srcPath & d
fso.CopyFile srcFile, destPath & d
Kill srcFile
d = Dir
Loop
Next x
MsgBox "done"
End Sub
解决方案
试试下面的代码。Test
我在你的里面加了两行,没MoveFiles_3
必要。
Sub Test()
Dim xWb As Workbook
Dim xToBook As Workbook
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Dim xFiles As New Collection
Dim I As Long
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder [Kutools for Excel]"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
If Right(xStrPath, 1) <> "\" Then xStrPath = xStrPath & "\"
xFile = Dir(xStrPath & "*.xlsx")
If xFile = "" Then
MsgBox "No files found", vbInformation, "Kutools for Excel"
Exit Sub
End If
Do While xFile <> ""
xFiles.Add xFile, xFile
xFile = Dir()
Loop
Set xToBook = ThisWorkbook
If xFiles.Count > 0 Then
For I = 1 To xFiles.Count
Set xWb = Workbooks.Open(xStrPath & xFiles.Item(I))
xWb.Worksheets(1).Copy after:=xToBook.Sheets(xToBook.Sheets.Count)
On Error Resume Next
ActiveSheet.Name = xWb.Name
On Error GoTo 0
xWb.Close False
Name xStrPath & xFiles.Item(I) As "C:\Users\userfolder\Desktop\Archive Test\" & xFiles.Item(I) ' new added'
Kill xStrPath & xFiles.Item(I) ' new added'
Next I
End If
End Sub
推荐阅读
- laravel - Laravel 8 分页搜索无法正常工作
- python - 如何将 %20 转换为 sring 中的空格?
- javascript - JWT 身份验证不适用于网站,但适用于“邮递员”
- reactjs - 将此类转换为功能组件
- java - 如何在 Android Lollipop 中使用 Base64.getUrlEncoder
- google-cloud-platform - GCP AI Platform Job 无法导入本地模块
- javascript - 如何禁用右键单击反应引导卡?
- xcode - 带有默认 Xcode 12 Core Data 项目的空白白屏
- python - Django Rest Framework中的对象不是可迭代错误
- php - 电子邮件验证连接被拒绝