+ Reply to Thread
Results 1 to 5 of 5

Subtotal macro

Hybrid View

  1. #1
    Registered User
    Join Date
    11-25-2021
    Location
    Estonia
    MS-Off Ver
    365
    Posts
    2

    Subtotal macro

    Hi

    I need to program a macro that uses subtotals.
    The problem is that i need the subtotals to sort by multiple columns. I have a macro that reads data and writes the data in the sheet but i cant find a way to make subtotal rows that consider multiple rows. I can only find a way to create subtotal rows based on either row A or row B (in the example picture: either by Animal change or by color change, but i need it to create the rows by all columns). And also the subtotal (Data-Subtotal) app is adding all the subtotals to different rows but i need them to be on the same row.
    So i was wondering if there is a way to do it since i havent been able to find any help by googling?

    I dont have permissions to post the original data but i added an illustration to what am trying to achieve
    Attached Images Attached Images

  2. #2
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Subtotal macro

    Hi @henxerz
    Let's say your data is in columns B to E
    You can use an auxiliary column F
    joint here the 3 cells
    F2= B2 & C2 & D2
    so, you get a column with
    DogBrownBig
    DogBrownSmall
    CatBlueSmall
    etc..
    Now you can sort the table by this columnn and make subtotals for every change in this column

    finally delete the auxiliary column F
    Last edited by vichopalacios; 02-09-2023 at 12:40 PM.
    Barriers are there for those who don't want to dream

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,567

    Re: Subtotal macro

    Which number needs to be excluded to arrive at a total of 45 instead of 47?
    If it is the three that is in your 2nd picture but not in the first, it still does not jive.
    Experience trumps academics every day of the week and twice on Sunday.

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,567

    Re: Subtotal macro

    Long winded but easy to understand and also easy to maintain/change.
    Sub Or_Maybe_So()
    Dim lr As Long, i As Long, ttl As Long
    Dim rng As Range
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    ttl = WorksheetFunction.Sum(Cells(2, 4).Resize(lr - 1))
    Application.ScreenUpdating = False
        For i = lr To 3 Step -1
            If Cells(i, 1).Value <> Cells(i - 1, 1).Value Or _
                Cells(i, 2).Value <> Cells(i - 1, 2).Value Or _
            Cells(i, 3).Value <> Cells(i - 1, 3).Value Then Cells(i, 1).Resize(, 4).Insert Shift:=xlDown
        Next i
        For Each rng In Range("A2:D" & Rows.Count).SpecialCells(xlConstants).Areas
            With rng.Cells(1).Offset(rng.Rows.Count)
                .Value = "Subtotal"
                .Font.Bold = True
            End With
            With rng.Cells(1).Offset(rng.Rows.Count, rng.Columns.Count - 1)
                .Formula = "=Sum(R[-" & rng.Rows.Count & "]C:R[-1]C)"
                    .Value = .Value
                .Font.Bold = True
            End With
        Next rng
        With Cells(Rows.Count, 1).End(xlUp).Offset(1)
            .Value = "Total"
            .Font.Bold = True
                With .Offset(, 3)
                    .Value = ttl
                    .Font.Bold = True
                End With
        End With
    Application.ScreenUpdating = True
    End Sub

  5. #5
    Registered User
    Join Date
    11-25-2021
    Location
    Estonia
    MS-Off Ver
    365
    Posts
    2

    Re: Subtotal macro

    I run into a problem with this script when i have a gap in the data coulms, i have to sort it by columns A:B + L:O etc the ones in between wont be used to sort. And the amount column is K

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sorting by Alpha, Adding Subtotal - then removing subtotal - variable data sets
    By gregg_grug in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-03-2019, 07:41 PM
  2. subtotal macro: to subtotal only a few amounts
    By annabanna in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-25-2014, 01:36 PM
  3. Replies: 1
    Last Post: 09-18-2013, 04:10 PM
  4. Replies: 8
    Last Post: 07-31-2012, 09:41 AM
  5. Subtotal - Delete zero Subtotal and prior rows that calculate to that zero Subtotal
    By Whatsherface in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2012, 08:37 PM
  6. subtotal macro
    By osaben in forum Excel General
    Replies: 20
    Last Post: 02-25-2011, 09:16 AM
  7. Macro Subtotal Please Help!
    By mgmisola in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-16-2010, 12:15 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1