Name

customviews.Add(ViewName, [PrintSettings], [RowColSettings])

Synopsis

Creates a new view for the workbook.

Argument

Settings

ViewName

The name of the view to create.

PrintSettings

True saves printer settings with the view; False omits them. Default is True.

RowColSettings

True saves hidden cell settings with the view; False omits them. Default is True.

For example, the following code creates a new view that prints all worksheets in landscape orientation. The code also restores the original view:

Sub CreateLandscapeView(  )
    Dim ws As Worksheet, vw As CustomView, wb As Workbook
    Set ws = ActiveSheet
    Set wb = ThisWorkbook
    ' Save current settings.
    wb.CustomViews.Add ("Default")
    ' Set worksheets to print landscape.
    For Each ws In wb.Worksheets
        ws.PageSetup.Orientation = xlLandscape
    Next
    ' Save print settings as a view.
    wb.CustomViews.Add "Landscape", True, False
    ' Restore the previous settings.
    wb.CustomViews("Default").Show
End Sub

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.