Loop Through Sheets based on condition

shennricks

New Member
Joined
Apr 1, 2022
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Currently I have a formula that adds up all of the same cell on multiple sheets.

Eg:

=SUM('Sheet1:Sheet12'!K3)


What I would Ideally like to do is have corresponding columns on the front page where the user can select the sheets that they would like included in the summed data.

So there will be a column with the sheet names and then a column where the user can "x" next to it indicating it should be included in the data.

Is there an easy way to update the equation so it will loop through the range of sheets and check the condition based on a corresponding cell value on the first sheet?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
VBA Code:
Public Sub SheetsSum()

Dim Sum1 As Long: Sum1 = 0
Dim i As Integer
Dim RangeOnOtherSheetsToSum As String: Let RangeOnOtherSheetsToSum = "A1"
Dim StartingXPosition As String: Let StartingXPosition = "B2"
Dim PlaceSumResult As String: Let PlaceSumResult = "D3"
Dim ResultsWorksheet As String: Let ResultsWorksheet = "Sheet1"

Application.ScreenUpdating = False

    For i = Range(StartingXPosition).Row To Sheets.count
    
        If Worksheets(ResultsWorksheet).Cells(i, Range(StartingXPosition).Column).Value = "x" Then
        
            Sum1 = Sum1 + Sheets(i).Range(RangeOnOtherSheetsToSum).Value
            
        End If
        
    Next i

Worksheets(ResultsWorksheet).Range(PlaceSumResult).Value = Sum1

Application.ScreenUpdating = True


End Sub

Make sure to change the variables that fit your own spreadsheet.
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,172
Members
449,071
Latest member
cdnMech

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