• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Create a Report in Excel using VBA

Rasool Bux

New Member
Dear All,

I need help to create a report/sheet from the data with subtotals of three levels (cluster_no, District, Province) using vba through a button in excel. The data entry was done by subcluster level (uploaded file). This is a sample of just three columns and actual data contains more than 50 columns in a table range.

Please help me in this regard.
Thanks
 

Attachments

  • Sample1.xlsx
    15 KB · Views: 4
Thanks for suggestions. I know through Pivot table, but I want to make a button to add rows of subtotal in the same sheet for different levels using VBA.

Please help me in this regard.
Best Regards,
Rasool Bux
 
What's your expected output. Construct manually and upload.

If using Subtotal Grouping available in Excel... remember that it can't be used on Table range and it must first be converted to regular range. (By the way, multi-level subtotal grouping will look messy).

You can also use macro recorder for the process and modify to suite your need.
 
I have done calculations manually just for one province. The subclusters are not fixed for each cluster i.e. 2, 3, 4 subclusters per cluster.

Please see the uploaded file.
 

Attachments

  • Sample1.xlsx
    26.2 KB · Views: 3
How about this format. It uses native Excel Subtotal Grouping.
upload_2016-10-27_9-4-32.png
And can easily be reversed to return to original format.

Code will be like below.
Code:
Sub rangeSubTotal()

Application.ScreenUpdating = False

With ActiveSheet.Cells(1).CurrentRegion
    .Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(5, 6, 7), _
        Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    .Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(5, 6, 7), _
        Replace:=False, PageBreaks:=False, SummaryBelowData:=True
    .Subtotal GroupBy:=4, Function:=xlSum, TotalList:=Array(5, 6, 7), _
        Replace:=False, PageBreaks:=False, SummaryBelowData:=True
End With

ActiveSheet.Cells(1).CurrentRegion.ClearOutline

Application.ScreenUpdating = True

End Sub

Sub revertSubTotal()

ActiveSheet.Cells(1).CurrentRegion.removeSubTotal

End Sub

See attached.
 

Attachments

  • SubTotal.xlsb
    21.5 KB · Views: 15
Thank you very much, that's what I want but I have more than 50 columns. how I can use if I have range table instead of array(5,6,7) with header row.

I have created a userform for data entry of that table.
 
So each number in Array() represents column# within range that SUBTOTAL calculation is done on.

Replace with array of column# for your actual table.

Same goes for "GroupBy:=#" replace with actual column# where info is stored.
 
Back
Top