Converting SUMs to SUBTOTALs

Everyone knows that SUBTOTAL ignores filtered rows. Readers of DDoE know that SUBTOTAL also ignores other SUBTOTAL formulas. I tell everyone who will listen about the benefits of SUBTOTAL. It’s one of the best received tips in the ‘Tips and Tricks’ portion of the training I do. But I still get spreadsheets that use SUM and individual adding of cells. When I do, I convert them to SUBTOTAL to make sure there are no errors. Today, I decided to automate that process.

I’ve filled column B over to the right into column C so I can preserve the original data.

With Excel’s color coding and this simple worksheet, you may have spotted the error in the grand total formula. Below is the code I wrote to correct this situation without having to put in all the SUBTOTALs manually.

This won’t work in every situation, but this layout is the one I see the most. This layout being SUMs for the subtotals and a big =A1+A2+A3 style formula for the grand total.

Once again SUBTOTAL saves the day and fixes the error. The most common error I see with this layout is in the grand total, but not always. Sometimes the subtotals don’t cover the correct range. It would seem easier when replacing the SUMs to use the same range the SUM uses, but I wanted to make sure I fixed any of those errors too. To do that, I SUBTOTAL from the cell below the previous SUBTOTAL to the cell above the current one.

Pro tip: Use

to toggle between viewing formulas and values (that’s an accent grave, left of the 1 key on US keyboards).

6 thoughts on “Converting SUMs to SUBTOTALs

  1. A note on your test for a single column. While putting together my code that filters by all selected values ( I noticed that Selection.Columns.Count doesn’t always work.

    That’s because it only catches the first area of the Selection. For instance if I select $A$1:$A$5,$B$7:$B$10, Selection.Columns.Count returns 1.

    I came up with this test instead:

    This works because Address encompasses all the areas of a range.

  2. Good one Doug. I’ll probably adjust to check that Areas.Count = 1. I don’t need it to work on exotic layouts.

  3. I used:

  4. And if you are not a skilled VBA pro

    Ctrl + F
    Find What =SUM(
    Replace with =SUBTOTAL(109,
    Replace all

    For the Grand total
    Delete the original formula and Alt + =

  5. I found that you could accomplish majority of work with Find/Replace. Search for SUM(
    replace with

Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.