Итак, я работаю над этим проектом, в котором я хочу открыть все файлы в определенной папке, запустить макрос, который скопирует и вставит все данные из «листа 1» из этих файлов в «калькулятор» рабочей книги с рабочей таблицей. MRG '.
Итак, у меня есть базовый c код для открытия каждого файла 1 за один раз, но когда я пытаюсь запустить свой макрос, я получаю
Ошибка 'Требуется объект'
на open.wb
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
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)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Change First Worksheet's Background Fill Blue
'wb.Worksheets(1).Range("A1:Z1").Interior.Color = RGB(51, 98, 174)
CopyPasteMRG
'Save and Close Workbook
wb.Close SaveChanges:=False
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'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
'CopyPasteMRG' is the macro i want to run on each file; the error comes from wb.open within 'CopyPasteMRG'
Sub CopyPasteMRG()
'
'need code to activate the correct opened workbook
'has to find the range start and end by itself
wb.Open
Range("A1:BV59").Select
Selection.Copy
Application.CutCopyMode = False
Selection.Copy
Application.WindowState = xlNormal
Windows("Ex-Pakistan Calculator Final.xlsm").Activate
Sheets("MRG").Select
'has to find the last row by itself
ActiveWindow.SmallScroll Down:=294
Range("A301").Select
ActiveSheet.Paste
Windows("INMAA JAN 2020.xlsx").Activate
End Sub
Я новичок в VBA и могу использовать любую помощь, какую только смогу, спасибо.