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

Falko26

Board Regular
Joined
Oct 13, 2021
Messages
99
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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,224,915
Messages
6,181,725
Members
453,064
Latest member
robatthe2A

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