Excel VBA Add Total Row to Table: A Complete Guide
In this article
To add a total row to an Excel table using VBA, use the ShowTotals property of the ListObject object. By setting ActiveSheet.ListObjects("TableName").ShowTotals = True, Excel automatically appends a summary row to the bottom of the table, allowing you to use built-in functions like SUM, AVERAGE, or COUNT to aggregate your data instantly.
What You'll Need
- Microsoft Excel installed on a Windows or Mac computer.
- A dataset already formatted as an official Excel Table (Insert > Table).
- The Developer tab enabled in the Excel Ribbon to access the Visual Basic Editor.
- Basic familiarity with how to open a module in VBA.
Step-by-Step
- Open the Visual Basic Editor: Press
Alt + F11on your keyboard. This shortcut opens the VBA environment where you will write the script. If you are on a Mac, you may need to use the Developer tab and click the Visual Basic icon. - Insert a New Module: In the Project Explorer window on the left, right-click on your workbook name, hover over
Insert, and selectModule. This creates a blank space for your code to live, ensuring it is available to the entire workbook. - Identify Your Table Name: Before writing the code, go back to your Excel sheet and click anywhere inside your table. Go to the
Table Designtab in the ribbon. On the far left, look at theTable Namebox. Note this name exactly (e.g., "Table1" or "SalesData"), as VBA is case-sensitive when referencing specific object names. - Write the Basic Activation Code: In the module window, type the following subroutine:
Sub AddTotalRow() Dim myTable As ListObject Set myTable = ActiveSheet.ListObjects("Table1") myTable.ShowTotals = True End SubReplace "Table1" with the actual name of your table. This code tells Excel to find the specific table on the active sheet and toggle the total row visibility to "On". - Define Specific Calculation Functions: By default, Excel may just show the count or a blank space in the total row. To specify a calculation (like a Sum) for a specific column, you need to reference the
TotalsCalculationRowproperty. Add these lines inside your subroutine:myTable.ListColumns("Amount").TotalsCalculationRow = xlSum myTable.ListColumns("Quantity").TotalsCalculationRow = xlAverageIn this example, the column named "Amount" will show the total sum, and the column named "Quantity" will show the average. - Run the Macro: You can run the code by pressing
F5while your cursor is inside the subroutine, or by returning to the Excel sheet, going to theDevelopertab, clickingMacros, selecting "AddTotalRow", and clickingRun. - Save the Workbook: Because your file now contains VBA code, you must save it as an
Excel Macro-Enabled Workbook (.xlsm). If you save it as a standard .xlsx file, the code will be permanently deleted.
Common Mistakes
One of the most frequent errors is referencing a table name that does not exist on the ActiveSheet. If your macro is running while a different sheet is selected, VBA will throw a "Subscript out of range" error because it cannot find "Table1" on the current view. To prevent this, it is better to reference the sheet explicitly using Worksheets("Sheet1").ListObjects("Table1") instead of ActiveSheet.
Another common mistake is attempting to add a total row to a range of cells that looks like a table but hasn't been officially converted. VBA's ListObject properties only work on official Excel Tables. If you have a standard range of data, you must first convert it using ActiveSheet.ListObjects.Add before you can enable the ShowTotals property.
Users also frequently forget that the ShowTotals property is a toggle. If the total row is already visible and you run myTable.ShowTotals = True, nothing changes. However, if you write a script that simply toggles the state without specifying True, you might accidentally hide the totals when you intended to show them.
Tips
To make your macro more dynamic, you can use a loop to apply the SUM function to every numeric column in the table automatically. This is useful for large tables where you don't want to manually name every column in your code. You can iterate through myTable.ListColumns and check if the data type is numeric before applying xlSum.
For a more professional look, you can use VBA to format the total row. While the total row has a default style, you can use myTable.TotalsRowRange.Font.Bold = True or change the interior color of the total row to match your corporate branding. This ensures the summary data stands out from the rest of the dataset.
If you are building a dashboard, consider adding a button to the worksheet. Go to the Developer tab, select Insert, and choose the Button (Form Control). Draw the button on your sheet and assign it to the "AddTotalRow" macro. This allows non-technical users to generate the totals without ever seeing the VBA editor.
Troubleshooting
If the code runs but no total row appears, first check if the table is protected. If the worksheet is protected, VBA cannot modify the table structure. You must add ActiveSheet.Unprotect "YourPassword" at the start of your code and ActiveSheet.Protect "YourPassword" at the end.
If you receive a "Run-time error 9: Subscript out of range", double-check the spelling of your table name. Remember that "Table 1" (with a space) is different from "Table1" (without a space). Check the Table Design tab to verify the exact string.
If the total row appears but the values are 0 or incorrect, check the data type of the column. If numbers are stored as text, the xlSum function will ignore them. You can fix this by selecting the column, going to the Data tab, and using Text to Columns to convert the data back to numbers before running the macro.
If the VBA editor freezes or crashes, ensure you aren't running an infinite loop. If you used a While or For Each loop to set totals for multiple columns, make sure the loop has a clear exit condition. Press Ctrl + Break to force the code to stop running.
Frequently Asked Questions
Can I use this to add totals to multiple tables at once?
Yes. You can create a For Each loop that iterates through all ListObjects in a worksheet. By using For Each tbl In ActiveSheet.ListObjects, you can set tbl.ShowTotals = True for every table on the page in one click.
What are the different calculation options available?
VBA provides several constants for the TotalsCalculationRow property. Common ones include xlSum for addition, xlAverage for the mean, xlCount for the number of entries, xlMax for the highest value, and xlMin for the lowest value.
Does adding a total row affect Pivot Tables?
No. A total row is a visual summary at the bottom of a source table. It does not interfere with Pivot Tables, but if your Pivot Table source range is a Table, the total row is automatically excluded from the Pivot data to prevent double-counting.
Can I remove the total row using VBA?
Yes. To remove or hide the total row, simply set the property to false: myTable.ShowTotals = False. This is often used in "Reset" macros that clear formatting before importing new data.
Will this work in Excel Online?
No. VBA macros only run in the desktop versions of Excel for Windows and Mac. Excel Online supports Office Scripts (TypeScript), which is a different language. You would need to rewrite the logic using the Excel JavaScript API for web-based automation.
How do I handle tables that change size?
The beauty of the ListObject is that it is dynamic. Once ShowTotals is set to True, the total row will automatically shift down as you add new rows to the table, and the calculations will update in real-time without needing to rerun the macro.