+ Reply to Thread
Results 1 to 2 of 2

Need a macro that will cycle through list then filter, subtotal, and print the list

Hybrid View

  1. #1
    Registered User
    Join Date
    01-12-2012
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Need a macro that will cycle through list then filter, subtotal, and print the list

    I am needing a macro that will cycle throught the Dept No's in range (A7:A24) of Dept_List worksheet. For each Dept No.I am then needing the data on "GL_Detail" Worksheet to filter Column C for each Dept No., then subtotal by "FS Description", sum "Amount", Print, then start over with the next Dept No.
    Any help is greatly appreciated.
    Attached Files Attached Files

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Need a macro that will cycle through list then filter, subtotal, and print the li

    Hi Crimson Bourne

    This code is in the attached. See it it does as you require. Let me know of issues.
    Option Explicit
    Sub test()
        Dim LR As Long
        Dim Rng As Range
        Dim afRng As Range
        Dim cel As Range
        Dim x As Long
    
        Application.ScreenUpdating = False
        With Sheet1
            LR = .Range("C" & .Rows.Count).End(xlUp).Row
            Set Rng = .Range("A6:H" & LR)
    
            For Each cel In Sheets("Dept_List").Range("Dept_No")
                With Rng
                    .AutoFilter Field:=3, Criteria1:=cel
                End With
    
                Set afRng = Sheet1.AutoFilter.Range
                x = Rng.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
                If x >= 1 Then
                    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Temp"
                    afRng.Copy
                    Sheets("Temp").Range("A6").PasteSpecial Paste:=8
                    Sheets("Temp").Range("A6").PasteSpecial Paste:=xlValues
                    Sheets("Temp").Range("A6").PasteSpecial Paste:=xlFormats
                    Selection.Subtotal GroupBy:=5, Function:=xlSum, TotalList:=Array(8), _
                            Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    
                    With ActiveSheet.PageSetup
                        .Orientation = xlLandscape
                        .FitToPagesWide = 1
                        .FitToPagesTall = 1
                    End With
    
                    ' swap these two lines of code to print or to print preview
                    '        ActiveSheet.PrintOut
                    ActiveSheet.PrintPreview    'for debugging
                Else
                End If
                Sheet1.AutoFilterMode = False
                On Error Resume Next
                Application.DisplayAlerts = False
                Sheets("Temp").Delete
                Application.DisplayAlerts = True
                Sheet1.Activate
                On Error GoTo 0
            Next cel
        End With
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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