Tricky VBA Loop Through Excel Table Range and Change Total Column Formula

Falko26

Board Regular
Joined
Oct 13, 2021
Messages
89
Office Version
  1. 365
Platform
  1. Windows
All,

I have a tricky VBA Code Scenario.

I'm trying to define a table range then loop through all columns in said table range and change the formula in the total row of the table.

The Tricky Part is my Table Self Updates so the number of columns and the label of the columns change depending on the project.

Step one: Define range starting in Column 10 of table and including all remaining columns.
Step Two: For each columns in range select Total Row and paste Formula. Formula needs to include column Header value so we will need to define this as a variable.

Defining what to do with the columns is where I'm lost.

Current Code:
VBA Code:
Sub Macro3()

    Dim oSheetName As Worksheet
    Dim sTableName As String
    Dim loTable As ListObject
      
    'Define Variable
    sTableName = "Table_Earned31"

    'Define WorkSheet object
    Set oSheetName = Sheets("Project Header")
    
    'Define Table Object
    Set loTable = oSheetName.ListObjects(sTableName)
    
    loTable.ListColumns(10).DataBodyRange.Resize(, ActiveSheet.ListObjects("Table_Earned31").HeaderRowRange.Columns.Count - 9).Select


    Dim rng As Range: Set rng = loTable.ListColumns(10).DataBodyRange.Resize(, ActiveSheet.ListObjects("Table_Earned31").HeaderRowRange.Columns.Count - 9)
    Dim col As Range
    For Each col In rng.Columns

    'Define what to do with each column
    
    Variable = HeadersRowRange
    
    TotalsRowRange.Select
    ActiveCell.FormulaR1C1 = _
    "=SUMIF([UNIT],Table_Earned31[[#Totals],[UNIT]]," & Variable & ")"
    
    Next col

1661186990673.png
 

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.
You are trying to set a SUMIF formula; could you tell us which is the If interval, which is the Criteria, and which is the Sum interval?
Also, is "Table_Earned31" the name of the shown table?
 
Upvote 0
Sure Anthony,

The Column "Unit" is the If Interval
The Column "Unit" Total Cell is the Criteria
The Columns with Dates in the header are the Sum Intervals

The hard part is the Dates and number of columns change automatically depending on the project you select. I have code that deletes and adds columns depending on the project selected. Because of this simply putting the sum if formula into the cells in question does not work, at least I couldn't get it to work. So my idea was to reset the formulas inside the VBA Code. I tried to do a loop command "For each cell in range Paste said formula" with the header date being the variable that changes. My take on that code is shown in the picture below.

1661276674284.png
 
Last edited:
Upvote 0
Try with this snippet:
VBA Code:
Dim sTableName As String
Dim loTable As ListObject
Dim rng As Range, TRRange As Range

sTableName = "Table_Earned31"

Set oSheetName = Sheets("Project Header")
Set loTable = oSheetName.ListObjects(sTableName)

Set rng = loTable.HeaderRowRange
Set TRRange = loTable.TotalsRowRange

For I = 10 To rng.Columns.Count
    TRRange.Cells(1, I).Formula = "=SUMIF([UNIT]," & TRRange.Cells(1, 2).Address & ",[" & rng.Cells(1, I).Value & "])"
Next I
 
Upvote 0
Solution
Wow that works perfectly!

A lot less complicated than I was imagining.

Thanks so Much Anthony!!
 
Upvote 0

Forum statistics

Threads
1,214,548
Messages
6,120,141
Members
448,948
Latest member
spamiki

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