Dynamic Sum Range (column) for SUMIF inserted via VBA

jwb1012

Board Regular
Joined
Oct 17, 2016
Messages
167
Hello, I am trying to use VBA to populate a summary table based on inputs within the "staffing plan" worksheet. Right now, I am using an array formula to populate these hours per resource per month summaries, but it takes FOREVER.

I was wondering if it would be possible to use a regular SUMIF formula instead, but the one thing that would need to happen is that the sum column would need to be dynamic off of the column header. The column headers are in F-Q (there are multiple summary tables on top of each other - 2016 then 2017 then 2018, etc., so the column headers are in multiple rows). I currently reference the column headers with "F$" & N_2 - 1" below which is dragged across to column Q ("Q$" & N_2 - 1").

I'm hoping for something like:
= SUMIFS("dynamic sum column - rows 13:1008", 'Staffing Plan'!$C$13:$C$1008,$A$1,'Staffing Plan'!$L$13:$L$1008,$C" & N_2)

For reference... in column A, Row 1 of each summary table there is a number which determined and inserted the number of rows required for each summary table. So, the column headers are always in row: "N_2" - 1.

Code:
Sub Generate_4()
    Dim Sh As Worksheet
    Dim Source_End_Row_2 As Integer
    Dim Insert_Rows_2 As Integer
    Dim N2 As Integer

                        For Each Sh In ActiveWorkbook.Sheets
                            If Left(Sh.Name, 9) = "Labor BOE" Then
                            
                                'Populate the "Labor Hours By Resource" summary at WBS Level
                                Source_End_Row_2 = Sh.Range("T" & Rows.Count).End(xlUp).Row
                                For N_2 = Source_End_Row_2 To 3 Step -1
                                    Insert_Rows_2 = Sh.Cells(N_2, "A").Value
                                    
                                        If Insert_Rows_2 > 0 Then
                                        
                                            'MONTH COLUMNS
                                            On Error Resume Next
                                            With Sh.Range("F" & N_2, "Q" & N_2 + Insert_Rows_2)
                                                .Formula = "=SUM(IF('Staffing Plan'!$C$13:$C$1008=$A$1,IF('Staffing Plan'!$L$13:$L$1008=$C" & N_2 & ",IF('Staffing Plan'!$L$13:$FV$13=[U][B]F$" & N_2 - 1 [/B][/U]& ",'Staffing Plan'!$L$13:$FV$1008))))"
                                                .FormulaArray = .FormulaR1C1
                                                .Value = .Value
                                            End With
                                            On Error GoTo 0
                                            
                                        End If
                            Next N_2
                        End If
                    Next Sh
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,223,920
Messages
6,175,376
Members
452,638
Latest member
Oluwabukunmi

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