简体   繁体   中英

Vba: Print an Excel sheet to multiple pdf pages

I'll try to keep this short. I'm working on a Excel project and I have to print at the end a single sheet, the problem is the printed PDF is too small, so I've looked up for it in different forums and I found out that I had to turn .FitToPagesTall = 0 In order that the excel worksheet don't get fit to one pdf page. The problem I'm struggling with now is that even if the pdf pages are bigger than before, It still small and It's making it hard to read. My idea is to print each 30 rows (for example) in a pdf page (Page1 --> Range("A1:E30"), Page2 --> Range("A31:E60").. etc, you got the idea)

Any ideas how I can do that please?

Thanks in advance !

Update #1: Here's a screen shot of only a slice of my data range在此处输入图像描述

Even if I set the .PrintArea to A:D, it still give the same result. To rephrase my request: I'm looking for a way to print different ranges in multiple pages.

thanks everyone for giving me a part out of ur time.

try to adapt the Orientation and fit the data horizontally, something like this :

With ActiveSheet.PageSetup
    .PrintTitleRows = "$1:$1" 'to repeat your header on each page 
    .CenterHorizontally = True
    .CenterVertically = True
    .Orientation = xlLandscape ' or xlPortrait
    .Zoom = False
    .FitToPagesWide = 1 'fit to wide only
    .FitToPagesTall = 0
End With

EDIT: auto-selection code added

My understanding of the question: how to print multiple selections from the same worksheet into PDF containing multiple pages (each selection = new page)

Multiple selection (ctlr) & Print Selection

Range("A1:E30, A31:E60") _ 
    .PrintOut Copies:=1, Collate:=True  'your ranges in the quotes
'prompts user to choose destination where to save to

It is equal to manually selecting multiple ranges (holding ctrl ) and selecting print selection when printing.

打印选择

Edit - Code added

If you like the above idea, you might like the following code?

It constructs a string , such as: "A1:D30,A31:D60,A61:D90" , which specifies ranges to be selected. Assumptions is that data is in columns AD and last row can be found in column A. You can specify how many rows to print in my_step .

Sub select_and_print()

Dim s_rn, s_range As String
Dim i, my_step As Integer
Dim selections_count As Single

my_step = 30 'how many rows to include on 1 page

'to prevent extra page in some cases:
selections_count = WorksheetFunction.RoundUp((Cells(Rows.Count, 1).End(xlUp).Row / my_step), 0)

'String, which specifies selections in ws, between columns A - D
For i = my_step To selections_count * my_step Step my_step:
    s_rn = "A" & (i - my_step + 1) & ":D" & i
    If i = my_step Then
        s_range = s_rn
    Else
        s_range = s_range + "," + s_rn
    End If
Next i

'selection & print
Range(s_range).PrintOut Copies:=1, Collate:=True 

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM