Select Case Error Problem

bisel

Board Regular
Joined
Jan 4, 2010
Messages
233
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I am trying to use SELECT CASE to run a simple bit of VBA to change the zoom level of the worksheets in my workbook.

Here is the VBA ...


VBA Code:
Private Sub ComboBox2_Change()
Dim wrksht As Worksheet


Select Case Sheet5.Range("mag_setting").Value
    Case Is = "95%"
        For Each wrksht In ThisWorkbook.Worksheets
        wrksht.Activate
        ActiveWindow.Zoom = 95 ' Set zoom level to 95% for each worksheet

    Case Is = "100%"
        For Each wrksht In ThisWorkbook.Worksheets
        wrksht.Activate
        ActiveWindow.Zoom = 100 ' Set zoom level to 100% for each worksheet

    Case Is = "110%"
        For Each wrksht In ThisWorkbook.Worksheets
        wrksht.Activate
        ActiveWindow.Zoom = 110 ' Set zoom level to 110% for each worksheet

    Case Is = "125%"
        For Each wrksht In ThisWorkbook.Worksheets
        wrksht.Activate
        ActiveWindow.Zoom = 125 ' Set zoom level to 125% for each worksheet

    Case Is = "150%"
        For Each wrksht In ThisWorkbook.Worksheets
        wrksht.Activate
        ActiveWindow.Zoom = 150 ' Set zoom level to 150% for each worksheet

End Select
    

Sheet16.Activate 'activate the home sheet
    
Application.ScreenUpdating = True

End Sub

But when I attempt to run the macro, I get this error message ...

1710542955100.png


Appreciate any help.

Regards,

Steve
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try to remove the "Is =" in each case statement.

VBA Code:
Private Sub ComboBox2_Change()
   Dim zoomLevel As Double
   Dim wrksht As Worksheet
 
    Select Case ComboBox2.Value
       Case "95%": zoomLevel = 95
       Case "100%": zoomLevel = 100
       Case "110%": zoomLevel = 110
       Case "125%": zoomLevel = 125
       Case "150%": zoomLevel = 150
       Case Else: Exit Sub
   End Select
 
    For Each wrksht In ThisWorkbook.Worksheets
       wrksht.Activate
       ActiveWindow.Zoom = zoomLevel
   Next wrksht
 
    Sheet16.Activate
 
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
You don't need the Select Case statements or even iterating the worksheets:
VBA Code:
Private Sub ComboBox2_Change()
    Application.ScreenUpdating = False
    ThisWorkbook.Worksheets.Select
    ActiveWindow.Zoom = CInt(Replace(Sheet5.Range("mag_setting").Value, "%", ""))
    Sheet16.Activate 'activate the home sheet
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
I was going to suggest similar after finding out what type of ComboBox it was.
 
Upvote 0
Try to remove the "Is =" in each case statement.

VBA Code:
Private Sub ComboBox2_Change()
   Dim zoomLevel As Double
   Dim wrksht As Worksheet
 
    Select Case ComboBox2.Value
       Case "95%": zoomLevel = 95
       Case "100%": zoomLevel = 100
       Case "110%": zoomLevel = 110
       Case "125%": zoomLevel = 125
       Case "150%": zoomLevel = 150
       Case Else: Exit Sub
   End Select
 
    For Each wrksht In ThisWorkbook.Worksheets
       wrksht.Activate
       ActiveWindow.Zoom = zoomLevel
   Next wrksht
 
    Sheet16.Activate
 
    Application.ScreenUpdating = True
End Sub
Thanks for help
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top