Issues subtotaling and breaking down the subtotal outline to obtain records for copying.

Chris101

New Member
Joined
Feb 17, 2023
Messages
28
Office Version
  1. 365
Platform
  1. Windows
I download each month a report that provides a list of telephone numbers that I keep track of to show how many incoming calls were made to one of our call centers and the duration of those calls.

What I am doing is placing the downloaded data into the worksheet "Original Data". From There I move the data to the sheet labeled "Raw Data" where I sort from lowest to highest on the column "F" ("Transfer Dest") and then subtotal (sum) on column "J" ("Duration").

Where I am having an issue is that I cannot write vba to collapse the outline to give me only the Total Transfer Dest numbers. Once I have only the records for the Total Transfer Dest numbers, I do not know how to copy those records and paste them into the sheet labeled Import to DB. When I try to paste the numbers, it seems to paste all of the telephone numbers and not just the telephone numbers that have totals. For example, I sort and subtotal on the worksheet "Raw Data Sheet with sort and subtotal" which is attached for reference. Then what I would like to do is collapse the outline to have only the records that have the totals appearing in column "F" ("Transfer Dest"). An example is located in the worksheet "sourcexferusage..." which is attached for reference. lastly I would like to copy and paste those records into the sheet labeled "Sheet import to DB" See sample workseet "Sheet Import to DB..." for reference.

In my example data i have 20 records. After I collapse the outline i have 3 records. when i try and paste those records, I end up pasting 22 records instead of the 3.

I have attached my code for the sort and subtotal but I have given up on trying to figure out how to collapse the outline and copy only those records appearing in the outline.

I know I am asking for a lot of assistance on this project, and I greatly appreciate your time and assistance in helping me resolve this problem.

Thank you in advance for your time and effort.

Here is my code so far:
VBA Code:
Option Explicit
Sub subtotal()

Dim wsh7 As Worksheet
Dim lastrow As Integer
Dim rng As Range

Set wsh7 = ThisWorkbook.Sheets("Raw Data")

wsh7.Activate
'wsh7.Range("A1").EntireRow.Delete
    
wsh7.Columns.AutoFit
    
     lastrow = wsh7.Cells(wsh7.Rows.Count, "A").End(xlUp).Row
     
 With wsh7
    With .Sort
        .SortFields.Clear
        .SortFields.Add2 Key:=wsh7.Range("F2:F" & lastrow), SortOn:=xlSortOnValues, _
            Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange wsh7.Range("A2:L" & lastrow)
        '.Header -xlYes
        '.Orientation -xlTopToBottom
        .Apply
    End With


Set rng = .Range("A1:L" & lastrow)


rng.subtotal groupby:=6, Function:=xlSum, totalList:=Array(10), Replace:=True, _
pagebreaks:=False, summarybelowdata:=True

End With
End Sub
 

Attachments

  • Original Data.png
    Original Data.png
    191 KB · Views: 7
  • Raw Data sheet With Sort and Subtotal.png
    Raw Data sheet With Sort and Subtotal.png
    192.6 KB · Views: 6
  • Worksheet sourceXferUsage - What I am trying to achieve.png
    Worksheet sourceXferUsage - What I am trying to achieve.png
    118.9 KB · Views: 7
  • Sheet Import to DB is the final product prior to DB load.png
    Sheet Import to DB is the final product prior to DB load.png
    114.9 KB · Views: 7

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Forum statistics

Threads
1,223,883
Messages
6,175,168
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top