You are here:

Excel/Loop - Visible sheets selection

Advertisement


Question
Hi Tom,

I have a workbook containing multi sheets.
some of those sheets was hidden (Because it's data sheets#.
example:
Sheet 1 --------------"Data sheet 1" ------ Hidden
Sheet 2 --------------"Main Sheet" -------- Visible
Sheet 3 --------------"Charts sheet" ------- Visible
Sheet 4 --------------" Data sheet 2" ------ Hidden
Sheet 5 --------------" HR info sheet" ----- Visible
Sheet 6 --------------" Admin info sheet" --- Visible
Sheet 7 and 8 and 9 ....14 will be visible and from sheet 15 till sheet 19 will be hidden.

I added one button in sheet 2 #which is main sheet# to show #present# my sheets one by one using following code:
Sub Button1_Click##
Dim WS_Count As Integer
         Dim I As Integer
         Set usersheet = ActiveSheet
         Application.DisplayFullScreen = True
         Application.DisplayAlerts = False
         
         WS_Count = ActiveWorkbook.Worksheets.Count

        For I = 1 To WS_Count
         Application.ScreenUpdating = False
         
         ActiveWorkbook.Worksheets#I#.Select
         Application.ScreenUpdating = True
         
        If MsgBox#"OK for next Sheet, Cancel to stop.", _
         vbQuestion + vbOKCancel# = vbCancel Then Exit For
         
        Next I
   
End Sub

My problem was: above code need to show all sheets #hidden & visible# and it will show error if I keep some of them hidden.

Can you help be developing my code to capture only those #Visible) sheets and ignoring the hidden sheets.
Thanks

Answer
Mahmoud,

just put in an if statement to see if the sheet is visible

Sub Button1_Click##
Dim WS_Count As Integer
         Dim I As Integer
         Set usersheet = ActiveSheet
         Application.DisplayFullScreen = True
         Application.DisplayAlerts = False
         
         WS_Count = ActiveWorkbook.Worksheets.Count

        For I = 1 To WS_Count
         if activeworkbook.worksheets(I).Visible = xlSheetVisible then
         Application.ScreenUpdating = False
         
         ActiveWorkbook.Worksheets(I).Select
         Application.ScreenUpdating = True
         
         If MsgBox#"OK for next Sheet, Cancel to stop.", _
         vbQuestion + vbOKCancel# = vbCancel Then Exit For
         End if
        Next I
   
End Sub


--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.