我想循环遍历文件夹中的所有工作簿,从工作表“Import fil”的A列:CZ中复制数据,从第5行开始,向下复制到A列的最后一个活动行。然后将数据作为值粘贴到另一个工作簿“TOT_Importfiler.xlsm”的工作表“Blad1”中。每个新工作簿中的数据都应粘贴到TOT文件中的下一个空行上。此外,我想将每个工作簿中的工作簿名称添加到TOT文件DA列中该工作簿的所有行中,以便跟踪数据来自哪个工作簿。(我最好将工作簿名称放在TOT文件的A列中,并从工作簿中复制数据,从B列开始,但在最后添加也可以)。
我使用了另一篇文章中的代码,但我不知道如何添加工作簿名称。此外,它粘贴公式而不是值,这导致错误时,有一个链接到另一个工作簿,我没有访问。
有人能帮我吗?
Sub LoopAllExcelFilesInFolder()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim lRow As Long
Dim lRow2 As Long
Dim ws2 As Worksheet
Dim y As Workbook
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "C:\Importfiler test"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xlsx*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
Set y = Workbooks.Open("C:\Importfiler test\TOT_Importfiler.xlsm")
Set ws2 = y.Sheets("Blad1")
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Copy data on "SearchCaseResults" sheet to "Disputes" Sheet in other workbook
With wb.Sheets("Import fil")
lRow = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A5:CZ" & lRow).Copy ws2.Range("A" & Rows.Count).End(xlUp)(2)
End With
wb.Close SaveChanges:=True
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
2条答案
按热度按时间ie3xauqp1#
从关闭的工作簿导入数据
yvfmudvl2#
修改以下代码行
要在上面一行之后使用以下代码添加文件名