+ Reply to Thread
Results 1 to 2 of 2

Loop through an autofilter and export a PDF for each option

Hybrid View

  1. #1
    Registered User
    Join Date
    06-29-2016
    Location
    England
    MS-Off Ver
    Office 2015
    Posts
    9

    Loop through an autofilter and export a PDF for each option

    Hi,

    I've got a spreadsheet that I have information in which I need to loop through as follows :-

    Store Area Manager Head of Sales Other info

    I need to loop through all area managers and print info on all their stores to a pdf which relates to their name, and then the same for all area managers and export with their name.

    I'm stuck other than recording it, and that won't help me if the Names or number of AM changes etc.

    Hopefully someone can help ?

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,273

    Re: Loop through an autofilter and export a PDF for each option

    The way to do that is to find the unique values in the correct field from the data source, and loop through them. For example, if your data is on a sheet and your tow columns of managers are in A and B, with headers in A1 and B1, and column AA is empty, then code like this will find all the current values from A print them to a pdf in the same folder as the Excel file, then do the same for B.

    Sub Macro2()
        Dim rngC As Range
        Dim rngF As Range
        
        With ActiveSheet
            'Find the unique values from column A
            Set rngF = .Range(.Range("A1"), .Cells(.Rows.Count, "A").End(xlUp))
            rngF.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("AA1"), Unique:=True
            'Loop through the unique values
            For Each rngC In .Range(.Range("AA2"), .Cells(.Rows.Count, "AA").End(xlUp))
                rngF.AutoFilter Field:=1, Criteria1:=rngC.Value
                .ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                    ThisWorkbook.Path & "\" & rngC.Value & ".pdf", Quality:= _
                    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
                    OpenAfterPublish:=False
                rngF.AutoFilter
            Next rngC
            .Range("AA:AA").Clear
            'Find the unique values from column B
            Set rngF = .Range(.Range("B1"), .Cells(.Rows.Count, "B").End(xlUp))
            rngF.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("AA1"), Unique:=True
            'Loop through the unique values
            For Each rngC In .Range(.Range("AA2"), .Cells(.Rows.Count, "AA").End(xlUp))
                rngF.AutoFilter Field:=1, Criteria1:=rngC.Value
                .ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                    ThisWorkbook.Path & "\" & rngC.Value & ".pdf", Quality:= _
                    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
                    OpenAfterPublish:=False
                rngF.AutoFilter
            Next rngC
            .Range("AA:AA").Clear
        End With
    End Sub
    Last edited by Bernie Deitrick; 07-20-2016 at 04:30 PM.
    Bernie Deitrick
    Excel MVP 2000-2010

+ 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. Can someone advise if this export option is even possible?
    By Heath82 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-08-2015, 05:15 AM
  2. Export Option to Excel or PDF
    By tmill29 in forum Access Programming / VBA / Macros
    Replies: 0
    Last Post: 06-11-2014, 09:16 AM
  3. Sum each autofilter criteria and export to a new spreadsheet
    By Hjahren in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-13-2010, 04:59 AM
  4. Removing option of BLANK from autofilter
    By xlman in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-25-2010, 01:15 AM
  5. [SOLVED] On export option where you want to save
    By Muaitai in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-16-2006, 11:35 PM
  6. Missing Autofilter option !!
    By anandmr65 in forum Excel General
    Replies: 4
    Last Post: 06-26-2006, 04:15 PM
  7. How do I get the data/xml/export option
    By Nick in forum Excel General
    Replies: 2
    Last Post: 08-11-2005, 12:05 AM

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