VBA code to open files in a folder and copy from Sheet "Master" DK1:EA1 to DK2:EA2

jskasango

Board Regular
Joined
Jul 18, 2012
Messages
202
Office Version
  1. 365
Platform
  1. Windows
Hi, I am looking for assistance with a VBA code to open files in a folder and copy from Sheet "Master" DK1:EA1 to DK2:EA2, save and close.
I need this to run so that after it finishes I can then run a code that I was given by maabadi.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
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
 
Upvote 0
Solution
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
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?
 
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,108
Members
449,205
Latest member
ralemanygarcia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top