+ Reply to Thread
Results 1 to 6 of 6

Subtotal only group duplicates if value in column

Hybrid View

Chickenbones Subtotal only group... 02-18-2021, 09:30 PM
jindon Re: Subtotal only group... 02-19-2021, 01:58 AM
Chickenbones Re: Subtotal only group... 02-19-2021, 11:20 AM
davesexcel Re: Subtotal only group... 02-19-2021, 02:28 AM
Chickenbones Re: Subtotal only group... 02-19-2021, 11:17 AM
jindon Re: Subtotal only group... 02-19-2021, 11:28 AM
  1. #1
    Registered User
    Join Date
    12-21-2020
    Location
    Atlanta, GA
    MS-Off Ver
    2016
    Posts
    3

    Subtotal only group duplicates if value in column

    Y'all, I'm stumped. I am trying to format daily bank activity and I can't figure out how to do this last part. I have the data sorted the way I want, but now I need to get insert two rows under every change of value in column G where if there are duplicate values and then sum column D (debits) in one of new rows enter image description hereonly if there is a value in it. Then I need to do the same thing for column E (credits). For any unique values in column G, I just need to insert a line. I do this on a daily basis, so the solution needs to be for a macro. The format must match what the results picture looks like. Can someone please point me in the right direction?
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Subtotal only group duplicates if value in column

    Try the attached.
    Sub test()
        Dim x, i As Long
        Application.ScreenUpdating = False
        With Sheets("sheet1")
            With .Range("g1", .Range("g" & Rows.Count).End(xlUp)(2))
                x = Filter(Parent.Evaluate("transpose(if(" & .Address & "<>" & _
                .Offset(1).Address & ",row(" & .Address & ")))"), False, 0)
            End With
            For i = UBound(x) To 1 Step -1
                If x(i - 1) - x(i) = 1 Then
                    .Cells(x(i) + 1, 1).EntireRow.Insert
                Else
                    .Cells(x(i) + 1, 1).Resize(2).EntireRow.Insert
                    .Cells(x(i) + 1, "d").Resize(, 2).Formula = _
                    "=if(count(d" & x(i - 1) + 1 & ":d" & x(i) & _
                    "),subtotal(9,d" & x(i - 1) + 1 & ":d" & x(i) & "),"""")"
                End If
            Next
            .Columns.AutoFit
        End With
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    12-21-2020
    Location
    Atlanta, GA
    MS-Off Ver
    2016
    Posts
    3

    Re: Subtotal only group duplicates if value in column

    Thank you Jindon for putting this macro together in the spreadsheet and including a button. That was a nice touch. Unfortunately, when I ran it the unique values were also summed. I do have another project that this will work for though. Thank you again!

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Subtotal only group duplicates if value in column

    When inserting rows, you need to start from the bottom and work up.
    Try this.

    Sub Get_r_Done()
    
        Dim sh As Worksheet, lr As Long, i As Long, x As Long, rng As Range, cel As Range, a, b, c, d
        Set sh = Sheets(1)                           'Edit sheet name
        lr = sh.Cells(Rows.Count, 1).End(xlUp).Row
        For i = lr To 2 Step -1
            With sh
                If .Cells(i, 7).Value = .Cells(i - 1, 7).Value Then
                    x = x + 1
                Else
                    Set rng = .Cells(i, 4).Resize(x + 1, 1)
                    Set cel = .Cells(i, 4).Offset(x + 1, 0)
                    cel.EntireRow.Insert
                    If rng.Count > 1 Then
                        a = Application.Sum(rng)
                        b = Application.Sum(rng.Offset(, 1))
                        c = IIf(a = 0, b, a)
                        d = IIf(a = 0, 1, 0)
                        cel.Offset(-1#, d) = c
                        cel.EntireRow.Insert
                        x = 0
                    End If
                End If
            End With
        Next
    End Sub

  5. #5
    Registered User
    Join Date
    12-21-2020
    Location
    Atlanta, GA
    MS-Off Ver
    2016
    Posts
    3

    Re: Subtotal only group duplicates if value in column

    Holy cow! This worked! Thank you so much, you are incredible!

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Subtotal only group duplicates if value in column

    OOps, of course...
    typo
                If x(i - 1) - x(i) = 1 Then
    should be
                If x(i) - x(i - 1) = 1 Then

+ 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. Group by Data and to do Subtotal.
    By ashrafk07 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-06-2017, 01:12 AM
  2. Group+Subtotal HOW?
    By toci in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-23-2015, 02:18 PM
  3. [SOLVED] Formula to Calculate Subtotal - Count of #'s Column While Avoiding Duplicates
    By swoosh1014 in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 10-30-2014, 10:09 AM
  4. remove duplicates in each subtotal group
    By trentq in forum Excel General
    Replies: 1
    Last Post: 04-08-2014, 04:02 AM
  5. Replies: 1
    Last Post: 10-19-2012, 07:55 AM
  6. How to Insert Group in Subtotal
    By koolguys4u in forum Excel General
    Replies: 1
    Last Post: 03-25-2012, 12:10 PM
  7. Group / Outline / subtotal
    By tikchye_oldLearner57 in forum Excel General
    Replies: 0
    Last Post: 06-22-2006, 06:50 AM

Tags for this Thread

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