Search code examples
excelvbazoomingactive-window

Setting zoom without select in Excel VBA


Is it possible to set up zoom level based on screen resolution without Select?

I have followed the code:

Sheets(1).Range("A1:AC1").Select
ActiveWindow.Zoom = True

taken from https://stackoverflow.com/a/19439177/1903793

Desired code would be something like:

Range("A1:AC1").Width.Zoom=True

Update. Why do I want to avoid Select?

  1. My sheet has hidden columns based on user settings. So some columns of the range A1:AC1 are hidden. I cannot select single column because this particular column may be hidden.
  2. Selection triggers events. Of course I can disable the events but disabling the events has some side effects which I want to avoid.

Solution

  • That is what I have ended up with. Solution is resistant to hidden columns. I do not select columns but shape. Add a rectangle, name it "BoxForZoom". It should be wide just enough for your zoom. Then apply the following code:

    Sheet1.Shapes("BoxForZoom").Visible = True
    Sheet1.Shapes("BoxForZoom").Select
    ActiveWindow.Zoom = True
    Sheet1.Shapes("BoxForZoom").Visible = False