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

Moving Subtotals

by Allen Wyatt
(last updated September 8, 2016)

6

David was adding subtotals (Data tab of the ribbon, in the Outline group) 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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8643) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Moving Subtotals.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Printing an Outline

Outlining is a great way to develop the content of your document. If you need to, you can even print the outline and only the ...

Discover More

Unprotecting Groups of Worksheets

Unprotecting a single worksheet is relatively easy. Unprotecting a whole lot of worksheets is harder. Here's how you can make ...

Discover More

Converting Text to Numbers

Import information from a program external to Excel, and your numbers may be treated as text because of the way that the ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Inserting a Sound File in Your Worksheet

Some worksheets are better understood through the spoken word or with musical accompaniment. Sound files can be easily ...

Discover More

Converting Imported Information to Numeric Values

If the information you import into Excel is treated as text by the program, you may want to convert it to numeric values. ...

Discover More

Accessing Dependent and Precedent Information

The auditing tools provided in Excel can provide some very helpful information about how your formulas and data are related ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 0 + 7?

2016-05-17 12:14:50

Sondra

This was just what I needed to move 17500+ subtotals one cell over. Thanks for the inspiration.


2014-12-02 18:35:32

Diedre

Outstanding! Just what I was looking for. Now, can anyone tell me how I can shade the SubTotal Row in my database. I assume I could do this using conditional formatting, but cannot work out the formula to use. Any help would be appreciated, as my data would look exceptional :)


2014-04-05 12:40:26

Dawn

I'm getting the same error, but I can't figure out the problem. It worked once with same code. Now it won't work. So frustrating. I could've manually done it by now.


2014-04-03 03:49:02

Celeste

I figured out the problem. Thanks anyway.


2014-04-03 03:24:42

Celeste

I am trying to move subtotals in a workbook that is the result of two worksheets merged, sorted, and subtotaled. I want to move the subtotals from column G to H. I changed the "S" to "G". I pasted your code and ran the macro and am getting a 424 error code at the line "For each rcell in rng". Any ideas why this is a problem? (p.s.: I am new to excel and macro code)


2014-03-03 05:33:38

Andrew

As it relates to subtotals I would also add the line -
rCell.Offset(0, iOffset).NumberFormat =
_
rCell.NumberFormat

to ensure that the format isn't lost


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.