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
'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 = "Select A Target Folder"
.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 = "*.xls*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Change the file that is open
Windows(myFile).Activate
Sheets("T & A").Select 'Select the Sheet
Range("D3").Select 'Set the Range
Selection.Copy 'Change the Active File Name
Windows("Dredger Summary Report.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'The next active cell will go to the offset
ActiveCell.Offset(0, 1).Select
'Next Instruction (Barge Volume)
'<<<<<<<<<<<<<Instruction Starts>>>>>>>>>>>>
'Change the file that is open
Windows(myFile).Activate
Sheets("T & A").Select
Range("F130").Select
Selection.Copy
Windows("Dredger Summary Report.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
'<<<<<<<<<<<<<<Instruction Ends>>>>>>>>>>>>>
'Next Instruction (Area)
'<<<<<<<<<<<<<Instruction Starts>>>>>>>>>>>>
'Change the file that is open
Windows(myFile).Activate
Sheets("Input").Select
Range("M12").Select
Selection.Copy
Windows("Dredger Summary Report.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
'<<<<<<<<<<<<<<Instruction Ends>>>>>>>>>>>>>
'Next Instruction (Material Type)
'<<<<<<<<<<<<<Instruction Starts>>>>>>>>>>>>
'Change the file that is open
Windows(myFile).Activate
Sheets("Input").Select
Range("AE12").Select
Selection.Copy
Windows("Dredger Summary Report.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
'<<<<<<<<<<<<<<Instruction Ends>>>>>>>>>>>>>
'Next Instruction (Depth Before)
'<<<<<<<<<<<<<Instruction Starts>>>>>>>>>>>>
'Change the file that is open
Windows(myFile).Activate
Sheets("Input").Select
Range("K12").Select
Selection.Copy
Windows("Dredger Summary Report.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
'<<<<<<<<<<<<<<Instruction Ends>>>>>>>>>>>>>
'Next Instruction (Depth After)
'<<<<<<<<<<<<<Instruction Starts>>>>>>>>>>>>
'Change the file that is open
Windows(myFile).Activate
Sheets("Input").Select
Range("J12").Select
Selection.Copy
Windows("Dredger Summary Report.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 2).Select
'<<<<<<<<<<<<<<Instruction Ends>>>>>>>>>>>>>
'Next Instruction (Dredging Depth)
'<<<<<<<<<<<<<Instruction Starts>>>>>>>>>>>>
'Change the file that is open
Windows(myFile).Activate
Sheets("Input").Select
Range("I12").Select
Selection.Copy
Windows("Dredger Summary Report.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
'<<<<<<<<<<<<<<Instruction Ends>>>>>>>>>>>>>
'Next Instruction (Operational Hour)
'<<<<<<<<<<<<<Instruction Starts>>>>>>>>>>>>
'Change the file that is open
Windows(myFile).Activate
Sheets("T & A").Select
Range("F86").Select
Selection.Copy
Windows("Dredger Summary Report.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 2).Select
'<<<<<<<<<<<<<<Instruction Ends>>>>>>>>>>>>>
'Next Instruction (Mechanical Maintenance)
'<<<<<<<<<<<<<Instruction Starts>>>>>>>>>>>>
'Change the file that is open
Windows(myFile).Activate
Sheets("T & A").Select
Range("F90").Select
Selection.Copy
Windows("Dredger Summary Report.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
'<<<<<<<<<<<<<<Instruction Ends>>>>>>>>>>>>>
'Next Instruction (Shifting Anchor)
'<<<<<<<<<<<<<Instruction Starts>>>>>>>>>>>>
'Change the file that is open
Windows(myFile).Activate
Sheets("T & A").Select
Range("F92").Select
Selection.Copy
Windows("Dredger Summary Report.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(1, -11).Select
'<<<<<<<<<<<<<<Instruction Ends>>>>>>>>>>>>>
'Save and Close Workbook
wb.Close SaveChanges:=False
'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
嗨,大家好, 所以我設法完善我的腳本,從不同的工作簿中的某些資料。但是我經常詢問和搜索一些關於代碼的問題。Excel中整合多個工作簿
問題: 如果你看看我的代碼,我每次將我的活動工作簿(這是我的目的地)名稱更改爲其他名稱,我必須在此行Windows(「Dredger Summary Report.xlsm」)下手動更改它。 。啓用。無論如何編寫一個代碼,將自動拿起積極的工作簿和活動工作表,沒有我不得不改變腳本中的名字,每次我改變我的文件名?
謝謝你,感謝所有輸入
'的Windows( 「挖泥船摘要Report.xlsm」)Activate'可以替換。 'ThisWorkbook.Activate',如果這是包含你的宏的工作簿。 –
請使用'ThisWorkbook'而不是'Windows(「Dredger Summary Report.xlsm」)' – Maddy