Macro to copy data from one workbook to all other workbooks in a specific folder

前端 未结 1 1649
挽巷
挽巷 2021-01-27 10:07

I need to copy a master project list from one workbook to all of the other workbooks in a specific folder. I have tried - see my macro below. While the macro does not give me an

相关标签:
1条回答
  • 2021-01-27 10:40

    Try this one:

    Sub Macro1()
       Dim file As String
       Dim myPath As String
       Dim wb As Workbook
       Dim rng As Range
    
       Dim wbMaster As Workbook
       'if master workbook already opened
       Set wbMaster = Workbooks("Master Project list (2).xlsx")
       'if master workbook is not opened
       'Set wbMaster = Workbooks.Open("C:\Users\New folder\Master Project list (2).xlsx")
    
       Set rng = wbMaster.Sheets("Master Project list").Range("A1:D34")
    
       myPath = "C:\Users\New folder\" ' note there is a back slash in the end
       file = Dir(myPath & "*.xls*")
       While (file <> "")
    
            Set wb = Workbooks.Open(myPath & file)
            rng.Copy
            With wb.Worksheets("Master Project list").Range("A1")
                .PasteSpecial xlPasteColumnWidths
                .PasteSpecial xlPasteAll
            End With
    
            wb.Close SaveChanges:=True
            Set wb = Nothing
    
            file = Dir
        Wend
    
        Application.CutCopyMode = False
    End Sub
    
    0 讨论(0)
提交回复
热议问题