+ Reply to Thread
Results 1 to 8 of 8

Macro that will loop through a filtered list and print each selection

  1. #1
    Registered User
    Join Date
    06-23-2014
    Location
    Grand Junction, CO
    MS-Off Ver
    2010
    Posts
    4

    Macro that will loop through a filtered list and print each selection

    I have a spreadshee that is always filtered in cell A9. I would like to create a macro to loop through each selection criteria and print. I have been able to create the initial macro but I need to be able to copy the macro to different worksheets/workbooks in the future and have it work. What is happening now is that I copy it and the pages that print are blank because it's looking for the selection criteria from the original sheet (the selection criteria will change from sheet to sheet but the filtered cell will always be A9). Any help would be greatly appreciated. Here is what I have:

    Please Login or Register  to view this content.
    Last edited by bobbysue25; 06-24-2014 at 10:42 AM.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro that will loop through a filtered list and print each selection

    Hi, bobbysue25,

    welcome to ExcelForum. Please be reminded that you would need to wrap your procedure with code-tags when being shown here according to Forum Rule #3

    You could either use WorksheetFunction.CountIf for the data range to make sure that an itrem exists or get the row number for the last visible row which should be greater than the title row in order to print:
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    06-23-2014
    Location
    Grand Junction, CO
    MS-Off Ver
    2010
    Posts
    4

    Re: Macro that will loop through a filtered list and print each selection

    Thank you for the response. The issue is that the criteria as well as the number of criteria are always changing. For example, "123456 ACT LLC" will not always be there. I need it to cycle through the filter selections no matter what the criteria. What is currently happening is when the macro is used on a new page (one page each day) the pages print out blank because it's looking for criteria that don't exist.

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro that will loop through a filtered list and print each selection

    Hi, bobbysue25,

    Your opening post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

    Ciao,
    Holger

  5. #5
    Registered User
    Join Date
    06-23-2014
    Location
    Grand Junction, CO
    MS-Off Ver
    2010
    Posts
    4

    Re: Macro that will loop through a filtered list and print each selection

    Okay, I think I fixed the way the code is displayed above. Thanks

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro that will loop through a filtered list and print each selection

    Hi, bobbysue25,

    both codes go into a standard module:
    Please Login or Register  to view this content.
    The Function will create a list of all unique items in Column A and then loop through the Collection in order to print. There must be no check as only those items being listed are printed.

    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    06-23-2014
    Location
    Grand Junction, CO
    MS-Off Ver
    2010
    Posts
    4

    Re: Macro that will loop through a filtered list and print each selection

    YOU ARE A GENIUS!!!!! THANK YOU SO MUCH!!! That worked perfectly on two different sheets. I get a printout for each different item as well as a blank one if the range is not completely full but I am perfectly fine with that. I have been able to verify the totals to make sure nothing was skipped over and this will save a lot of time

  8. #8
    Registered User
    Join Date
    09-17-2013
    Location
    Lebanon
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Macro that will loop through a filtered list and print each selection

    hello my HaHoBe,
    you wrote this macro some years ago, i hope you still remember it. i need to make it run on a different cell than A9. what shall I change in the macro ? (lets assume i want it to filter on the cell D12)

    Re: Macro that will loop through a filtered list and print each selection

    Hi, bobbysue25,

    both codes go into a standard module:
    [Copy to clipboard]

    Function myList(sh As String, lngCol As Long)
    Dim vntList(), n As Long, vntC, vntTmp
    Dim myCol As New Collection
    With Sheets(sh)
    ReDim vntList(1 To .Cells(Rows.Count, lngCol).End(xlUp).Row)
    vntTmp = .Range(.Cells(10, lngCol), .Cells(Rows.Count, lngCol).End(xlUp))
    End With
    For Each vntC In vntTmp
    Err.Clear
    On Error Resume Next
    myCol.Add vntC, CStr(vntC)
    If Err.Number = 0 Then
    n = n + 1
    vntList(n) = vntC
    End If
    Next
    ReDim Preserve vntList(1 To n)
    myList = vntList
    End Function
    Sub Printing()
    '
    ' Printing Macro
    '

    Dim lngLR As Long
    Dim varList As Variant
    '
    varList = myList(ActiveSheet.Name, 1)

    With ActiveSheet
    For lngLR = LBound(varList) To UBound(varList)
    .Range("A9").CurrentRegion.AutoFilter Field:=1, Criteria1:=varList(lngLR)
    .PrintOut Copies:=1
    Next lngLR
    End With
    End Sub

+ 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. Selection.Filldown doesn't work in a filtered list
    By lordterrin in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-20-2013, 02:39 PM
  2. How to print each filtered list on new page?
    By doubl3d80 in forum Excel General
    Replies: 0
    Last Post: 05-08-2013, 11:38 AM
  3. Automative Macro Print All Records in a Filtered List
    By kthumm11 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 10-16-2012, 11:55 AM
  4. Loop through a Filtered List
    By EggHead in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 10-24-2011, 03:10 PM
  5. Creating a filtered validation list based on selection of another
    By dgtwitch in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-21-2010, 09:20 PM

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