Resetting Window views in Excel

May 18, 2009JPNo CommentsRate This ArticlenewLinks to this article


    I hate getting workbooks in Page Break Preview mode. Okay, hate is a strong word, but it's annoying. I want my workbooks to be in Normal view, so I can edit them. It's especially annoying when you get a workbook with multiple worksheets, and they are all in page break preview mode. You have to manually go through each one and reset the view.

    Here's the code I came up with to fix the view for each worksheet. All we do is activate each worksheet, set the view, then return to the originally active worksheet. I would run code like this whenever someone sends me a workbook, to make sure all the sheets are in Normal view.

Sub ResetWindowView()

' store current worksheet, for later reference
Dim thisSht As Excel.Worksheet
Set thisSht = ActiveSheet

Dim sht As Excel.Worksheet
Dim currentWindow As Window

' loop through each worksheet and set view
For Each sht In Excel.Worksheets
  If TypeName(sht) = "Worksheet" Then
    sht.Activate
    Set currentWindow = ActiveWindow
    currentWindow.View = xlNormalView
  End If
Next sht

' go back to original sheet
thisSht.Activate

End Sub

About JP
I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space if you want to learn more about VBA. Keep Reading »

↑ Scroll to top
Previous Post:

Next Post:

2 Response(s) to Resetting Window views in Excel ↓

  1. Michael Pierce says:

    Nice little utility. I have a few questions for you:

    (1) Just in case the current sheet is a chart when you start the macro, what data type should thisSht be? I could only get it to work using a Variant type. (If a chart is selected, the current code blows up.)

    (2) Since you're cycling through Excel.Worksheets, why are you checking the TypeName()? Aren't they always going to be worksheets?

    (3) Is there a reason to use the currentWindow variable? Wouldn't just using the statement ActiveWindow.View = xlNormalView work just as well?

    • JP says:

      (1) 'As Object' worked for me, but it would be technically be a Chart object. So you could use As Object or As Variant.
      (2) You're right that the type check is redundant.
      (3) I like using intermediate variables, but you could easily do it your way and save a few lines of code.

Speak Your Mind

Tell us what you're thinking...

Certain comments (including first-time comments) are subject to moderation and will not appear immediately. Please view the Comment Policy for more information. To post VBA code in your comment, use tags like this: [cc lang='vb']Code goes here[/cc].




Site last updated August 24, 2010 @ 5:56 pm