我有多个(大约400个)受保护(使用相同密码)的excel文件,每个文件都有一个公用的工作表,名为“调查摘要”。我想将该工作表的数据(从特定列到数据末尾)从所有工作簿复制并追加到新的Excel工作簿。我使用了下面的VBA代码。不幸的是,它显示了运行时错误1004,表明“ Excel无法访问'TSSR已批准'。该文档可能是只读的或加密的。”
请帮助。
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com
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 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 = "F:\Nokia_BD\BanglaLink Project\TSSR\TSSR_Approved\Approved TSSR Complete"
.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("F:\Nokia_BD\BanglaLink Project\TSSR\TSSR_Approved\Approved TSSR Complete")
Set ws2 = y.Sheets("Survey Summary_Total")
'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 "Survey Summary" sheet to "Survey Summary_Total" Sheet in other workbook
With wb.Sheets("Survey Summary")
lRow = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A3:Q" & lRow).Copy ws2.Range("A3" & 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
谢谢 萨吉布
答案 0 :(得分:0)
如果文件受密码保护,则在打开文件时还需要输入密码:Application.Workbooks.Open Filename:=Filename, Password:=Password