jskasango
Board Regular
- Joined
- Jul 18, 2012
- Messages
- 202
- Office Version
- 365
- Platform
- Windows
Public Sub Copy_Range_In_Workbooks()
Dim matchWorkbooks As String
Dim folderPath As String
Dim wbFileName As String
Dim wb As Workbook
'Folder path and wildcard workbook files containing range to be copied
matchWorkbooks = "C:\folder\path\*.xls*" 'CHANGE THIS
Application.ScreenUpdating = False
folderPath = Left(matchWorkbooks, InStrRev(matchWorkbooks, "\"))
wbFileName = Dir(matchWorkbooks)
While wbFileName <> vbNullString
Set wb = Workbooks.Open(folderPath & wbFileName)
wb.Worksheets("Master").Range("DK1:EA1").Copy wb.Worksheets("Master").Range("DK2:EA2")
wb.Close savechanges:=True
wbFileName = Dir
Wend
Application.ScreenUpdating = True
MsgBox "Finished"
End Sub
Maybe it would be less cumbersome if I could be prompted for the folder, instead of having to edit the code each time I want to tell it to work on a folder?Try this macro, changing the folder path and wildcard file name spec as required.
VBA Code:Public Sub Copy_Range_In_Workbooks() Dim matchWorkbooks As String Dim folderPath As String Dim wbFileName As String Dim wb As Workbook 'Folder path and wildcard workbook files containing range to be copied matchWorkbooks = "C:\folder\path\*.xls*" 'CHANGE THIS Application.ScreenUpdating = False folderPath = Left(matchWorkbooks, InStrRev(matchWorkbooks, "\")) wbFileName = Dir(matchWorkbooks) While wbFileName <> vbNullString Set wb = Workbooks.Open(folderPath & wbFileName) wb.Worksheets("Master").Range("DK1:EA1").Copy wb.Worksheets("Master").Range("DK2:EA2") wb.Close savechanges:=True wbFileName = Dir Wend Application.ScreenUpdating = True MsgBox "Finished" End Sub
Search forprompted for the folder
Application.FileDialog(msoFileDialogFolderPicker)