+ Reply to Thread
Results 1 to 8 of 8

Select Case Problem

Hybrid View

  1. #1
    Registered User
    Join Date
    12-22-2011
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2007
    Posts
    5

    Select Case Problem

    Hey everyone, hopefully someone can save the rest of my hair here.

    I am trying to loop through 6 specific sheets and adjust their zoom level for displaying on a conference room TV. The code below works for the first 3 sheets wonderfully, but then errors out. If you step through it one line at a time, you can watch as it simply skips the last 3 sheets, as if they've already been done. The sheet names are definitely spelled correctly (copied directly from the tabs to be sure). I can't imagine what the heck I'm doing wrong. For now I'm just selecting each sheet individually and adjusting, but I am always trying to make my code more efficient wherever I can.

    Thanks in advance!

    Private Sub CommandButton3_Click()
    Dim ws As Worksheet, lngZoom As Long
     
    For Each ws In ThisWorkbook.Worksheets
         
        Select Case ws.Name
             
        Case "SIP Review": lngZoom = 250
        Case "MOR Dashboard": lngZoom = 200
        Case "Support Tickets": lngZoom = 210
        Case "Overall Score": lngZoom = 260
        Case "Outliers v1": lngZoom = 280
        Case "Outliers v2": lngZoom = 290
             
        End Select
        Application.ScreenUpdating = False
        With ws
            .Select
            ActiveWindow.Zoom = lngZoom
        End With
    Next ws
     
    Application.ScreenUpdating = True
    End Sub
    Last edited by outofahat; 09-12-2016 at 02:14 PM. Reason: Solved

  2. #2
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Select Case Problem

    can you upload a workbook? Any chance that last three sheets are actually chart sheets?
    If you are pleased with a member's answer then use the Star icon to rate it.

  3. #3
    Registered User
    Join Date
    12-22-2011
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Select Case Problem

    Test book attached (sensitive data in the original)

    Definitely no chart sheets in the original.
    Attached Files Attached Files

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Select Case Problem

    What's the error description?
    What line is highlighted when you click the debug button on the error dialog?
    Are any sheets hidden or protected?
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Registered User
    Join Date
    12-22-2011
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Select Case Problem

    Error: Method 'Select' of object '_Worksheet' failed.

    It's at the point where it is trying to .Select, but my Case Select is skipping over the last 3 sheets. So when it gets to .Select the 4th time, there's nothing to select.

    There are hidden sheets, but not any of the ones I am trying to select/zoom.

    Thanks, everyone.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Select Case Problem

    Quote Originally Posted by outofahat View Post

    There are hidden sheets, but not any of the ones I am trying to select/zoom.
    The .Select is after the Case code block. So it will try to .Select every sheet (including hidden sheets) regardless if it was a sheet within the Case code block.

    Try this...

    Sub excelforum_test()
    Dim ws As Worksheet, lngZoom As Long
    Application.ScreenUpdating = False
    For Each ws In ThisWorkbook.Worksheets
         
        Select Case ws.Name
             
        Case "SIP Review": lngZoom = 250
        Case "MOR Dashboard": lngZoom = 200
        Case "Support Tickets": lngZoom = 210
        Case "Overall Score": lngZoom = 260
        Case "Outliers v1": lngZoom = 280
        Case "Outliers v2": lngZoom = 290
        Case Else: lngZoom = 0
        End Select
        
        If lngZoom > 0 Then
            With ws
                .Select
                ActiveWindow.Zoom = lngZoom
            End With
        End If
    Next ws
    Application.ScreenUpdating = True
    End Sub

  7. #7
    Registered User
    Join Date
    12-22-2011
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Select Case Problem

    Brilliant! Thank you all very much!

    You guys never let me down!

  8. #8
    Valued Forum Contributor
    Join Date
    10-05-2014
    Location
    Regina, SK Canada
    MS-Off Ver
    Excel for Mac 11
    Posts
    330

    Re: Select Case Problem

    You may want to initialize lgnzoom to 100 to handle any other worksheet cases.

    or have a CASE ELSE that sets lgnzoom to 100.
    If this has been helpful, please click on the star at the left.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Select Case problem
    By saziz in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-31-2015, 03:50 PM
  2. [SOLVED] Select Case problem
    By Darthzo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-28-2012, 11:49 AM
  3. select case problem
    By jippii in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-11-2011, 06:05 PM
  4. select case problem
    By waltron in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-26-2010, 05:47 AM
  5. Long Select Case Problem
    By dsrt16 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-24-2009, 09:58 PM
  6. [SOLVED] Having Problem with Select Case
    By haroldj@shfoodbank.org in forum Excel General
    Replies: 2
    Last Post: 07-26-2006, 06:15 PM
  7. Select case and Last Cell Problem
    By JamesBurrows in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-05-2006, 12:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1