Grouping rows by criteria excel vba

olali

New Member
Joined
Jun 25, 2013
Messages
6
What I need;


I have a sales master data (with over 20 columns).

It contains the following:

S/N, Customer, Invoice date, Due date, vessel, producer, fiscal regime, quantity, price, sales value, date paid, amount paid, variance, remark, etc
1.) I need a macro to group this report by invoice type, sum each group, with the column header and group title for each group on same sheet.
2.) Copy 1 or more groups to form a another report on separate sheets
3.) Write the summary (like a pivot table) for some of the column headers. Eg, Fiscal regime, sum by each producer with columns quantity, sales value and receipts
4.) Group 1 or more of the summaries to form another report on separate sheets.
Not to write too much, I am able to provide more clarity when we start.

Thanks for your kind assistance. I have attached a sample of what I have done so far

VBA Code:
Option Explicit

Sub Filter_Restaurants()

    Dim c As Range, i, z, lr As Long
    Dim lastrow As Long, nextrow As Long
    
    Application.Calculation = xlCalculationManual
    Application.DisplayStatusBar = False
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    'Application.ScreenUpdating = False
    
    ' test for entries in the input range - exit sub if no entries
    If WorksheetFunction.CountA(Sheet1.Range("A1:A10")) < 1 Then Exit Sub
    
    'prepare output sheet to receive new filter results
    Sheet2.Cells.Clear
    
    With Sheet1
    
        lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
        
        ' count number of entries in the input cells
        i = WorksheetFunction.CountA(.Range("A1:A9"))
        
        'turn off any previous filters
        If .AutoFilterMode = True Then .AutoFilterMode = False
        
        'loop through input cells and filter and copy results
        For Each c In .Range("A1:A" & i)
            nextrow = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row + 3
            .Range("A10:A" & lastrow).AutoFilter field:=1, Criteria1:="=" & c.Value
            .Range("A10:P" & lastrow).SpecialCells(xlCellTypeVisible).Copy Sheet2.Range("A" & nextrow)
            
            With Worksheets("Sheet2")
            lr = .Cells(.Rows.Count, 1).End(xlUp).Row
            
            '.Cells(lr + 1, 1).Value = "Total"
            'lr = .Cells(.Rows.Count, 1).End(xlUp).Row
                    .Cells(lr + 1, 1).Value = "Total"
                    .Cells(lr + 1, 10).Resize(, 8).FormulaR1C1 = "=SUM(R3C:R[-1]C)"
            
            With .Range(.Cells(lr + 1, 1), .Cells(lr + 1, 16))
                .HorizontalAlignment = xlRight
                .Font.Bold = True
                .Interior.Color = rgbLightBlue
            End With
             '.Range("A & lastrow").CurrentRegion.Weight = xlThin
             
            End With
            
        Next c
        
        'turn off filtering
        .AutoFilterMode = False
    End With
    
    Sheet2.Activate
    Sheet2.Range("A4").Select
    
    Application.Calculation = xlCalculationAutomatic
    Application.DisplayStatusBar = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    'Application.ScreenUpdating = True

End Sub


Thank you.
 

Some videos you may like

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
5,939
Office Version
  1. 2019
Platform
  1. Windows

mole999

Moderator
Joined
Oct 23, 2004
Messages
10,524
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
@olali Your post has been reported as being cross posted with another site.

Whilst we do allow Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: > (Message Board Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.

Please provide the link(s) ASAP.
 

olali

New Member
Joined
Jun 25, 2013
Messages
6
Grouping rows by criteria excel vba


https://chandoo.org/forum/threads/vba-code-to-split-sales-report-sheet-to-groups-by-invoice-type-to-a-new-worksheet.44145/#post-263287

@olali
Your post has been reported as being cross posted with another site.

Whilst we do allow Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: > (Message Board Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.

Please provide the link(s) ASAP.
 

olali

New Member
Joined
Jun 25, 2013
Messages
6

 

olali

New Member
Joined
Jun 25, 2013
Messages
6
@olali Your post has been reported as being cross posted with another site.

Whilst we do allow Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: > (Message Board Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.

Please provide the link(s) ASAP.
Thank you very much. I didn't know about the rules. Please accept my apologies. it has not been solved elsewhere.

I admit sharing it on chandoo and excelforum and my intent was to widen my inquiry. Going forward, this rule will strictly be obeyed.

Thank you admin, I still look forward to a solution from you. Thank you.
 

Watch MrExcel Video

Forum statistics

Threads
1,114,649
Messages
5,549,179
Members
410,903
Latest member
natesreich
Top