Sum cell values from multiple workbooks with multiple worksheets - Macro

前端 未结 1 1025
野趣味
野趣味 2021-01-23 09:04
  • I have 50 workbooks. Each has 3 worksheets.
  • I have a value in each of these sheets from cells A1 to N1.
  • I want to sum up all the values in a single workb
相关标签:
1条回答
  • 2021-01-23 09:40

    If you want to sum values from all workbooks (from each worksheet A1:N1) and paste result in A1:N1 of thisWorkbook, use this one:

    Sub SUM_Workbooks()
        Dim FileNameXls, f
        Dim wb As Workbook, i As Integer
    
        FileNameXls = Application.GetOpenFilename(filefilter:="Excel Files, *.xl*", MultiSelect:=True)
    
        If Not IsArray(FileNameXls) Then Exit Sub
    
        Application.ScreenUpdating = False
    
        For Each f In FileNameXls
            Set wb = Workbooks.Open(f)
            For i = 1 To 3
                wb.Worksheets(i).Range("A1:N1").Copy
                ThisWorkbook.Sheets(i).Range("A1:N1").PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, SkipBlanks:=True, Transpose:=False
            Next i
            wb.Close SaveChanges:=False
        Next f
    
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    End Sub
    
    0 讨论(0)
提交回复
热议问题