Zoom Worksheets To Fit Used Range

  • Could someone please assist with helping to apply a zoom to Fit Selection on all worksheets upon file opening? I've read many posts on this and understand how to apply this via worksheet activation, but haven't discovered how to make this to happen to all worksheets when the file is opened.


    Zooming the selection needs to happen in the workbook module due to additional sheets being added at random and because it will facilitate the flow of the meeting so that we don't have to wait for the user to select and zoom.



    Thanks for helping.

  • Re: Zoom All Worksheets To Fit Selection On File Open


    Why dont you put it in the workbook_Sheetactivate event:


    Code
    1. Option Explicit
    2. Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    3. Dim rCell As Range
    4. With Sh
    5. Set rCell = .Cells.Find("*", Range("A1"), xlFormulas, , xlByColumns, xlPrevious)
    6. If rCell Is Nothing Then Exit Sub
    7. .Range(.Cells(1, 1), .Cells(1, rCell.Column)).EntireColumn.Select
    8. End With
    9. ActiveWindow.Zoom = True
    10. End Sub
  • Re: Zoom All Worksheets To Fit Selection On File Open


    Thanks Reafidy.


    My newbie colors are showing again. I didn't think to place it there.


    The code works great as long as the sheet has some data, but gives a "run-time error 91, object variable or With block variable not set" if the sheet is empty.


    My knowledge has not yet developed enough to fix this as I'm not sure what variable to set.
    I guess the approach is an IF/THEN to test for empty cells in the range and skip the zoom if so?


    I will appreciate any further help you can provide.