luckydzs
luckydzs

Reputation: 9

How to zoom in and out the active window using a scrollbar in excel vba

Private Sub ScrollBar1_Change()
    ActiveWindow.Zoom = ScrollBar1.Value
End Sub

Private Sub UserForm1_Initialize()
 With ScrollBar1
    .Min = 10
    .Max = 400
    .SmallChange = 1
    .LargeChange = 10
 End With
End Sub

I used the above code but get this run time error:

error 1004(unable to set the zoom property of the window class)

Upvotes: 0

Views: 853

Answers (3)

SwedishProgrammer
SwedishProgrammer

Reputation: 111

You could also post this part:

With ScrollBar1
    .Min = 10
    .Max = 400
    .SmallChange = 1
    .LargeChange = 10
 End With

In the same block as:

ActiveWindow.Zoom = ScrollBar1.Value

So it looks like this:

Private Sub ScrollBar1_Change()
ActiveWindow.Zoom = ScrollBar1.Value
With ScrollBar1
    .Min = 10
    .Max = 400
    .SmallChange = 1
    .LargeChange = 10
 End With
End Sub

Upvotes: 0

MatthewD
MatthewD

Reputation: 6801

This is due to the the zoom needing to be above 10. Do something like this.

Private Sub ScrollBar1_Change()
    Dim i As Integer

    i = ScrollBar1.Value
    If i <= 10 Then
        i = 10
    End If

    ActiveWindow.Zoom = i
End Sub

Upvotes: 0

Rory
Rory

Reputation: 34075

Move the code to the Activate event instead of the Initialize event so that it updates the initial value properly. The error is because the initial value is still 1 otherwise, and you can't zoom below 10%.

Upvotes: 1

Related Questions