Creating Totals row in table with VBA

unknown_user1337

New Member
Joined
Sep 16, 2021
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Having trouble finding the answer to this anywhere.. I need to add a Totals row to a dynamically created table with VBA, using a custom formula for the totals. Each row in the table has an "x" or is blank for each column, and I need to get the count of "x"'s for each column. What I have for code, after inserting data into a column:
VBA Code:
                dest.ListObjects(destTbl).ListColumns(col).TotalsCalculation = xlTotalsCalculationCustom
                dest.ListObjects(destTbl).ListColumns(col).Total.Formula = "=COUNTIF([" & col & "]," & Chr(34) & "x" & Chr(34) & ")"

This just returns an error for "Object variable or With block variable not set" on the last line above.. There is no documentation on how to use xlTotalsCalculationCustom, and I found the above solution buried somewhere in a stackoverflow answer.. So if this isn't right, what is??
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi & welcome to MrExcel.
What is col?
 
Upvote 0
Ok, in that case try
VBA Code:
   With dest.ListObjects(destTbl)
      .ShowTotals = True
      .ListColumns(col).Total.FormulaR1C1 = "=COUNTIF([" & col & "],""X"")"
   End With
 
Upvote 0
Solution
Ok, in that case try
VBA Code:
   With dest.ListObjects(destTbl)
      .ShowTotals = True
      .ListColumns(col).Total.FormulaR1C1 = "=COUNTIF([" & col & "],""X"")"
   End With
Alright, I got it. Seems the trick was to put .ShowTotals = True before the formula. Thanks!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

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