Print 2 worksheets into 1 pdf by vba

nancyma1440

New Member
Joined
Apr 30, 2018
Messages
1
Hello,
I have an excel file:
- For the 1st 100 worksheets called "1","2","3","4","5"..."100" are the debit notes to different companies, suppose the print area is A1:G77, default print setting is 1 page
- For the 101st worksheet called "Supporting" which is the page 2 of those debit notes & normally I have to use the filter to print the different data & attach with different debit notes, suppose the print area is A1:AI352, default print setting is 1 page (as there have ~10 rows for each debit note)
- For the 102nd worksheet called "Control" which is for reference only

Actually I have tried below & also success with correct format of output
1. print all debit note to 100 pdf (i.e. 100 pdfs & 1 page/file) which print area is A1:G77
2. print all supporting to 100 pdf (i.e. 100 pdfs & 1 page/file) which print area is A1:AI352

Now I am going to do is..... create 100 pdf files which have 2 pages, 1st page is the debit note & 2nd page is the supporting.
However, per my marco below, the 1st page of the output is OK, but the 2nd page printed the range of A1:G77 only.

** if I move the command "Sheets(Array((i - 1), "Supporting")).Select" to the below of "For i = 2 To tr", then
the output have 16 pages, which 1st is the correct debit note, 2nd-15th is blank, and 16th is correct support.

Code:
Sub PDF_Combine()
    
tr = Sheets("Control").Range("b1").End(xlDown).Row

For i = 2 To tr
    
    With Sheets(i - 1)
        .Activate
        .Range("a1:g77").Select
    End With
    
    With Sheets("Supporting")
        .Activate
        .Range("a1:ai350").AutoFilter 10, Sheets("Control").Range("d" & i)
        .Range("a1:ai352").Select
    End With
        
    Sheets(Array((i - 1), "Supporting")).Select
               
    Selection.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    filename:=Sheets("Control").Range("af" & i) & ".pdf", _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    openafterpublish:=False
            
Next i
 
End Sub


Could you please help on my issue?? Million thanks.
 
Last edited by a moderator:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top