Add cells from infinite number of spreadsheets in a folder

lemon123

New Member
Joined
Feb 27, 2019
Messages
4
I need to come up with something in VBA to essentially sum corresponding cells on identically formatted excel documents and output them onto a seperate master copy. Ie Spreadsheet 1, 2 and 3 all have information in cells A1, A2, B1, B2 so would like all the A1's to sum and output onto cell A1 on the master then the same for A2 etc etc.

Relative rookie with excel VBA, any help is greatly appreciated!
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I'm afraid Excel cannot deal with an infinite number of sheets :)
 
Upvote 0
Try this macro. Change the folderPath string as required, and it expects Spreadsheet 1, 2, etc. to be .xlsx files.

Code:
Public Sub Sum_Cells_In_Workbooks()

    Dim folderPath As String, fileName As String
    Dim masterSheet As Worksheet
    Dim openedWb As Workbook
    
    Set masterSheet = ActiveWorkbook.Worksheets(1)
    
    folderPath = "C:\path\to\folder\"
    If Right(folderPath, 1) <> Application.PathSeparator Then folderPath = folderPath & Application.PathSeparator
    
    Application.ScreenUpdating = False
    fileName = Dir(folderPath & "*.xlsx")
    Do While fileName <> vbNullString
        Set openedWb = Workbooks.Open(folderPath & fileName)
        With masterSheet
            .Range("A1").Value = .Range("A1").Value + openedWb.Worksheets(1).Range("A1").Value
            .Range("A2").Value = .Range("A2").Value + openedWb.Worksheets(1).Range("A2").Value
            .Range("B1").Value = .Range("B1").Value + openedWb.Worksheets(1).Range("B1").Value
            .Range("B2").Value = .Range("B2").Value + openedWb.Worksheets(1).Range("B2").Value
        End With
        openedWb.Close False
        fileName = Dir
    Loop
    Application.ScreenUpdating = True
    
    MsgBox "Done"

End Sub
 
Upvote 0
Thank you so much! Was also just wondering how to modify to make this work for Mac as well. Presumably this below will be the difference but unsure how to source a folder on Mac

Code:
    folderPath = "C:\path\to\folder\"
    If Right(folderPath, 1) <> Application.PathSeparator Then folderPath = folderPath & Application.PathSeparator
 
Upvote 0
Try this macro. Change the folderPath string as required, and it expects Spreadsheet 1, 2, etc. to be .xlsx files.

Code:
Public Sub Sum_Cells_In_Workbooks()

    Dim folderPath As String, fileName As String
    Dim masterSheet As Worksheet
    Dim openedWb As Workbook
    
    Set masterSheet = ActiveWorkbook.Worksheets(1)
    
    [U][I][B]folderPath = "C:\path\to\folder\"[/B][/I][/U]
    If Right(folderPath, 1) <> Application.PathSeparator Then folderPath = folderPath & Application.PathSeparator
    
    Application.ScreenUpdating = False
    fileName = Dir(folderPath & "*.xlsx")
    Do While fileName <> vbNullString
        Set openedWb = Workbooks.Open(folderPath & fileName)
        With masterSheet
            .Range("A1").Value = .Range("A1").Value + openedWb.Worksheets(1).Range("A1").Value
            .Range("A2").Value = .Range("A2").Value + openedWb.Worksheets(1).Range("A2").Value
            .Range("B1").Value = .Range("B1").Value + openedWb.Worksheets(1).Range("B1").Value
            .Range("B2").Value = .Range("B2").Value + openedWb.Worksheets(1).Range("B2").Value
        End With
        openedWb.Close False
        fileName = Dir
    Loop
    Application.ScreenUpdating = True
    
    MsgBox "Done"

End Sub

Is there any way to modify this by creating a button which has a Search Folder macro instead of requiring hardcoding of the specific folder? I have written a macro for the button (I think) but unsure how to link to this code to enable it to use the folder selected by the button. Thanks so much

Code:
 Sub FolderSelect()
    On Error GoTo Err
    Dim FileExplorer As FileDialog
    Set FileExplorer = Application.FileDialog(msoFileDialogFolderPicker)

    FileExplorer.AllowMultiSelect = False

   With FileExplorer
        If .Show = -1 Then
            [folderPath] = .SelectedItems.Item(1)
        Else
            [folderPath] = ""
        End If
    End With

Err:
    Exit Sub
End Sub
 
Upvote 0
Insert my code (without the Public Sub and End Sub lines) inside the If .Show = -1 clause, replacing folderPath = "C:\...." line with folderPath = .SelectedItems.Item(1).
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,161
Members
453,021
Latest member
Justyna P

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