Hi,
I'm new to VBA and have managed to created a macro to copy data from specific cells in a protected workbook (file1.xlsx) located in a folder (C:\Results) to a summary workbook (Summary.xlsm). I have up to 900 files in the Results folder and need to complete the same copy paste for each file. The filename for each file needs to be inserted into Column A and the first 2 rows contain headers so all pasted values will start from row 3 onward.
I'm having trouble repeating this macro for the rest of the files in the folder. Is anyone able to help me adapt my macro for multiple files?
Sub CopyCells()
Workbooks.Open filename:= _
"C:\Results\file1.xlsx" 'Open file in folder
ActiveSheet.Unprotect Password:="123" 'Remove password protection
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets.Item(1) 'Use only the first sheet in workbook
fNAME = ActiveWorkbook.Name
Range("G19:G21,G24:G26,G29:G31,G34:G36,G39:G41").Select
Selection.Copy 'Copy specific cells
Windows("Summary.xlsm").Activate 'Switch to summary excel file in current worksheet
Range("A3") = fNAME 'Insert file1 name into cell A3
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True 'Paste copied values from file worksheet into row 3 from column B onwards
End Sub
Many thanks!
I'm new to VBA and have managed to created a macro to copy data from specific cells in a protected workbook (file1.xlsx) located in a folder (C:\Results) to a summary workbook (Summary.xlsm). I have up to 900 files in the Results folder and need to complete the same copy paste for each file. The filename for each file needs to be inserted into Column A and the first 2 rows contain headers so all pasted values will start from row 3 onward.
I'm having trouble repeating this macro for the rest of the files in the folder. Is anyone able to help me adapt my macro for multiple files?
Sub CopyCells()
Workbooks.Open filename:= _
"C:\Results\file1.xlsx" 'Open file in folder
ActiveSheet.Unprotect Password:="123" 'Remove password protection
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets.Item(1) 'Use only the first sheet in workbook
fNAME = ActiveWorkbook.Name
Range("G19:G21,G24:G26,G29:G31,G34:G36,G39:G41").Select
Selection.Copy 'Copy specific cells
Windows("Summary.xlsm").Activate 'Switch to summary excel file in current worksheet
Range("A3") = fNAME 'Insert file1 name into cell A3
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True 'Paste copied values from file worksheet into row 3 from column B onwards
End Sub
Many thanks!