+ Reply to Thread
Results 1 to 10 of 10

Create Loop for Printing Reports

Hybrid View

  1. #1
    Registered User
    Join Date
    01-04-2012
    Location
    mpls mn
    MS-Off Ver
    Excel 2007
    Posts
    15

    Create Loop for Printing Reports

    I have attached a workbook with a report and code [Test-1] assigned to a macro button called Print All. Right now the code enters a value in cell A3, hides and unhides selected rows based on values in column A, prints the report, then moves on to report #2 (in cell A3). It does this 347 times. I am hoping to create a loop that executes the hide/unhide/print, then replaces the value in cell A3 and repeats the hide/unhide/print without repeating all the code. Any/all suggestions would be greatly appreciated. - TJ
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,996

    Re: Create Loop for Printing Reports

    My suggestion:
    Use the last 347 columns on the sheet as a row selection matrix. The first column would correspond to Report #1 and each row in the column would have a 1 if that row is to be hidden for that particular report.
    For instance column "WRV" i.e. #16038, would have a 1 in rows: 23, 24, 33 & 38. Then all you need is a loop counting from 1 to 347 that puts the count # in A3 and un-hides all rows, then re-hides only the rows with a 1 for that particular report.
    Sub PrtAll()
    
        Dim ReportNumber    As Long, _
            RowNumber       As Long
        
        For ReportNumber = 1 To 347
             Cells.Select
             Selection.EntireRow.Hidden = False
    
            For RowNumber = 1 To 98
                If Cells(RowNumber, ReportNumber+16037) = 1 Then
                    Cells(RowNumber, ReportNumber+16037).EntireRow.Hidden = True
                End If
            Next RowNumber
            Range("A3").Value = ReportNumber
            ActiveWindow.SelectedSheets.PrintOut Copies:=1
        Next ReportNumber
    End Sub 'prtall
    Last edited by protonLeah; 12-29-2012 at 03:01 AM.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    12-29-2012
    Location
    Baku
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Create Loop for Printing Reports

    Hi, I have similar problem, the only difference is I am trying to re-arrange table in Excel. So I have recorded a Macro and now trying to loop it. Below is the copy of recorded macro as well as the format of the tables I would like to create. On the left hand side is table, on the right hand side it is only for the first row. I would like to get a loop for each row. Many thanks before!
    Attached Images Attached Images
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    01-04-2012
    Location
    mpls mn
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Create Loop for Printing Reports

    This seems very simple and elegant - question: do I need to unhide rows after printing and before looping to the next report #? Thanks.

  5. #5
    Registered User
    Join Date
    01-04-2012
    Location
    mpls mn
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Create Loop for Printing Reports

    Also, the macro need to replace the value in A3 each time to update the data shown on the report - additional code needed? Thanks.

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,996

    Re: Create Loop for Printing Reports

    1. The line: Range("A3").Value = ReportNumber, updates A3 for each report.
    2. I only put in the Unhide code because I didn't know if the lines that were hidden in a particular report should remain hidden for all subsequent reports. If it is true that once a line is hidden, it remains hidden for all subsequent reports, then you don't need the unhide code.
    ===================================

    @Partisano:
    Welcome to the Forum, unfortunately:

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.

  7. #7
    Registered User
    Join Date
    12-27-2012
    Location
    Riyadh
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Create Loop for Printing Reports

    I have made a salary slip with a drop down liast usign data validation.. The drop down list is working fine and the vlookups are also alright. Now i need a button to print all the salary sheets together...

    Could any one help me with this one ?

    I have added the button but i do not know the coding thing to print them all at once.
    Looking forward for some positive replies...

  8. #8
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,996

    Re: Create Loop for Printing Reports

    :@shahmurtaza, You could not but see the post just above yours. You ignored it!


    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.

    If you don't intend to abide by the rules, then why post at all?

  9. #9
    Registered User
    Join Date
    12-27-2012
    Location
    Riyadh
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Create Loop for Printing Reports

    oops.. Apologies mate.. i really did not notice that one.

  10. #10
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,107

    Re: Create Loop for Printing Reports

    Thanks protonleah,

    others please read the rules and open your own threads and I will give infraction to those that continue spamming tjandas topic.
    Never use Merged Cells in Excel

+ 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