Print multiple custom views in one pdf with macro VBA code

IreneFoncillas

New Member
Joined
Oct 13, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am quite new with macros in excel, and I was wondering if it is possible to print multiple custom views in one pdf.

For example, I have two custom views named as "CV_1" and "CV_2". How can I print both custom views in a single pdf?

Also, would it be possible to print multiple sheets and custom views in the same pdf? Let's say I want to print, "Sheet 1"; "Sheet 2"; "CV_1"(which is in Sheet 3); "CV_2"(which is in Sheet 3); Sheet 4.

Thank you very much, I hope I have explained myself well
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Welcome to MrExcel forums.

Try this macro. The PDF is saved in the same folder as the macro workbook.

VBA Code:
Public Sub Save_Sheets_and_Custom_Views_As_PDF()

    Dim currentSheet As Worksheet
    Dim tempCVSheets(1 To 2) As Worksheet
    Dim i As Long
    
    With ThisWorkbook
    
        Set currentSheet = .ActiveSheet
        
        'Show CV_1 and copy Sheet3 to a temporary sheet
        
        .CustomViews("CV_1").Show
        .Worksheets("Sheet3").Copy After:=.Worksheets("Sheet3")
        Set tempCVSheets(1) = ActiveSheet
        
        'Show CV_2 and copy Sheet3 to a temporary sheet
        
        .CustomViews("CV_2").Show
        .Worksheets("Sheet3").Copy After:=tempCVSheets(1)
        Set tempCVSheets(2) = ActiveSheet
        
        'Select sheets and save as PDF
        
        .Worksheets(Array("Sheet1", "Sheet2", tempCVSheets(1).Name, tempCVSheets(2).Name, "Sheet4")).Select
       
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\Sheets_CustomViews.pdf", Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
        
    End With
    
    'Delete temporary custom view sheets
    
    Application.DisplayAlerts = False
    For i = 1 To UBound(tempCVSheets)
        tempCVSheets(i).Delete
    Next
    Application.DisplayAlerts = True
    
    currentSheet.Select
    
End Sub
 
Upvote 0
Thank you very much John for your quick response. It works perfectly!!! I have tried to modify sheets names and add more customs views and no error at all ?
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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