# Moving Subtotals

Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Moving Subtotals.

David was adding subtotals (Data | Subtotals) to large worksheets and looking for a way to move the subtotal cells to different cells. For instance, assume that when Excel added the automatic subtotals, they were added in column S, and the SUBTOTAL formula added by Excel referred to ranges of cells in column S. David wanted to move the SUBTOTAL formulas (and only those formulas) out of column S to column T, and have the formulas still refer to detail in column S.

One option is to go through and move the SUBTOTAL formulas, one at a time, to the desired locations. (You would use Ctrl+X and Ctrl+V to move the cells, rather than Ctrl+C and Ctrl+V to merely create copies of the cells.) If the worksheets are large, with many subtotals, this can become very tedious very quickly.

Tedium in Excel is often the primary impetus for creating a macro. This case is no exception. It is possible to create a macro that will do the actual move of the SUBTOTAL formulas. Consider the following example:

```Sub MoveSubtotals()
Dim rCell As Range
Dim rng As Range
Dim iCol As Integer
Dim iOffset As Integer

iCol = 19  '19 is Column S
iOffset = 1 'Positives go right, negatives go left
Set rng = Intersect(Selection.CurrentRegion, Columns(iCol))
For Each rCell In rng
If InStr(rCell.Formula, "SUBTOTAL") Then
rCell.Offset(0, iOffset).Formula = _
rCell.Formula
rCell.ClearContents
End If
Next
End Sub
```

This example works by examining each cell selected in column S. If the formula in the cell contains the word SUBTOTAL, then the formula is copied one column to the right, in column T, and deleted from the cell in column S. You can change the distance left or right that the subtotals are moved by simply changing the value assigned to the iOffset variable. (Note that you must run the macro with a range of cells selected, and part of that selection must include column S.)

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2119) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Moving Subtotals.

Related Tips:

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

 *Name: Email: Notify me about new comments ONLY FOR THIS TIP Notify me about new comments ANYWHERE ON THIS SITE Hide my email address *Text: *What is 5+3 (To prevent automated submissions and spam.)

Becky    13 May 2016, 17:42
This has saved me so much time in some of the spreadsheets I use, glad to find a quick solution
Jeff C    19 Mar 2015, 16:51
I tried your script, I keep getting an error message that an object is required in the rng line
For Each rCell In rng
Jeff C    19 Mar 2015, 16:49
I tried this script. I keep getting "need and object error message" on the rng line

# Our Company

Sharon Parq Associates, Inc.

# Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Excel Products

Word Products

# Our Authors

Author Index

Write for Tips.Net