?
Solved

Compile error: Next without For

Posted on 2016-09-28
9
Medium Priority
?
34 Views
Last Modified: 2016-11-09
I had this question after viewing Excel - Using a Sheet Tab as a Button for Expanding/Collapsing Supplementary Sheets..

Hi, I am not very familiar with coding, so I'm sure this is a simple fix.  I am trying to create sevral of these collapsing sheets within one workbook.  Each one will open up its own set of 12 months, So Sheet Name "A01" = January, "A02"= February, etc and my "Hide01, Hide02, Hide03" represents the expand/collapse sheets.  I am receiving a compile error and below is the code.  I tried adding "End If" after the 8th line of code (sheet.Visible = x1sheetvisible) and it allows the collapse/expand to work but it's not showing the correct tabs.  Can you please advise what I am missing?

Private Sub Worksheet_Activate()
    Dim sheet As Worksheet
    Application.ScreenUpdating = False
    If HIDE01.Name = "Show Sgt 1" Then
        'Make all sheets visible except the one's you want to keep hidden
        For Each sheet In ThisWorkbook.Sheets
            If (sheet.Name <> A01.Name And sheet.Name <> A02.Name And sheet.Name <> A03.Name And sheet.Name <> A04.Name And sheet.Name <> A05.Name And sheet.Name <> A06.Name And sheet.Name <> A07.Name And sheet.Name <> A08.Name And sheet.Name <> A09.Name And sheet.Name <> A10.Name And sheet.Name <> A11.Name And sheet.Name <> A12.Name And sheet.Name <> MVR.Name And sheet.Name <> HIDE01.Name And sheet.Name <> HIDE02.Name And sheet.Name <> HIDE03.Name) Then
                sheet.Visible = xlSheetVisible
        Next sheet
        'Change the sheet name to the "Collapse" name you want
        HIDE01.Name = "Hide Sgt 1"
        'Pick a sheet to display after the once hidden sheets are expanded
        A01.Activate
    Else
        'Hide all sheets except the one's you want to keep visible
        For Each sheet In ThisWorkbook.Sheets
            If (sheet.Name <> MVR.Name And sheet.Name <> HIDE01.Name And sheet.Name <> HIDE02.Name And sheet.Name <> HIDE03.Name) Then
               sheet.Visible = xlSheetVeryHidden
            End If
        Next sheet
        'Change the sheet name to the "Expand" name you want
        HIDE01.Name = "Show Sgt 1"
        'Pick a sheet to display after the sheets to be hidden are collapsed
        MVR.Activate
    End If
    Application.ScreenUpdating = True
End Sub


Thanks,
Christina
0
Comment
Question by:christina Ridings
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 41819920
Try...

        For Each sheet In ThisWorkbook.Sheets
            If (sheet.Name <> A01.Name And sheet.Name <> A02.Name And sheet.Name <> A03.Name And sheet.Name <> A04.Name And sheet.Name <> A05.Name And sheet.Name <> A06.Name And sheet.Name <> A07.Name And sheet.Name <> A08.Name And sheet.Name <> A09.Name And sheet.Name <> A10.Name And sheet.Name <> A11.Name And sheet.Name <> A12.Name And sheet.Name <> MVR.Name And sheet.Name <> HIDE01.Name And sheet.Name <> HIDE02.Name And sheet.Name <> HIDE03.Name) Then
                sheet.Visible = xlSheetVisible
            End If
        Next sheet
0
 

Author Comment

by:christina Ridings
ID: 41819931
I've done that and it hides the opposite of what I want openned.
0
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 41819998
I'm sorry, I didn't read the post thoroughly enough.  

Adding that line resolves the compilation error though?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:christina Ridings
ID: 41819999
I ended up adding the "End If" and then having to do the opposite sheets, which is weird, but it worked. Now it shows sheets A01:A12 when I click Show/Hide01.  This is going to be a bit of a pain to create since I'll have 10 Show/Hide sheets each with 12 hidden sheets, but I guess it'll work.

Private Sub Worksheet_Activate()
    Dim sheet As Worksheet
    Application.ScreenUpdating = False
    If HIDE01.Name = "Show Sgt 1" Then
        'Make all sheets visible except the one's you want to keep hidden
        For Each sheet In MVRChecklist.Sheets
            If (sheet.Name <> C01.Name And sheet.Name <> C02.Name And sheet.Name <> C03.Name And sheet.Name <> C04.Name And sheet.Name <> C05.Name And sheet.Name <> C06.Name And sheet.Name <> C07.Name And sheet.Name <> C08.Name And sheet.Name <> C09.Name And sheet.Name <> C10.Name And sheet.Name <> C11.Name And sheet.Name <> C12.Name And sheet.Name <> B01.Name And sheet.Name <> B02.Name And sheet.Name <> B03.Name And sheet.Name <> B04.Name And sheet.Name <> B05.Name And sheet.Name <> B06.Name And sheet.Name <> B07.Name And sheet.Name <> B08.Name And sheet.Name <> B09.Name And sheet.Name <> B10.Name And sheet.Name <> B11.Name And sheet.Name <> B12.Name And sheet.Name <> MVR.Name And sheet.Name <> HIDE01.Name And sheet.Name <> HIDE02.Name And sheet.Name <> HIDE03.Name) Then
                sheet.Visible = xlSheetVisible
            End If
        Next sheet
        'Change the sheet name to the "Collapse" name you want
        HIDE01.Name = "Hide Sgt 1"
        'Pick a sheet to display after the once hidden sheets are expanded
        A01.Activate
    Else
        'Hide all sheets except the one's you want to keep visible
        For Each sheet In MVRChecklist.Sheets
            If (sheet.Name <> MVR.Name And sheet.Name <> HIDE01.Name And sheet.Name <> HIDE02.Name And sheet.Name <> HIDE03.Name) Then
               sheet.Visible = xlSheetVeryHidden
            End If
        Next sheet
        'Change the sheet name to the "Expand" name you want
        HIDE01.Name = "Show Sgt 1"
        'Pick a sheet to display after the sheets to be hidden are collapsed
        MVR.Activate
    End If
    Application.ScreenUpdating = True
End Sub
0
 
LVL 34

Accepted Solution

by:
Paul MacDonald earned 2000 total points
ID: 41820005
I was going to suggest...

        For Each sheet In ThisWorkbook.Sheets
            If (sheet.Name <> A01.Name And sheet.Name <> A02.Name And sheet.Name <> A03.Name And sheet.Name <> A04.Name And sheet.Name <> A05.Name And sheet.Name <> A06.Name And sheet.Name <> A07.Name And sheet.Name <> A08.Name And sheet.Name <> A09.Name And sheet.Name <> A10.Name And sheet.Name <> A11.Name And sheet.Name <> A12.Name And sheet.Name <> MVR.Name And sheet.Name <> HIDE01.Name And sheet.Name <> HIDE02.Name And sheet.Name <> HIDE03.Name) Then
                sheet.Visible = !xlSheetVisible
            End If
        Next sheet

...(note the exclamation point in front of 'xlSheetVisible') but again, with out knowing more about what you're doing, it's difficult to say.
0
 
LVL 8

Expert Comment

by:Koen
ID: 41820042
sheets are referenced with an array... wouldn't it be a lot simpler to identify the correct array of sheets to select and then Hide/show ??
seems like you are writing a lot of code for something quite simple...
0
 

Author Comment

by:christina Ridings
ID: 41820050
Yes Koen, that's what I was trying to do but it wasn't working properly, I am not code savvy in the least, so I'm just shocked it's working.  I will try the exclamation point in sheet.Visible = !xlSheetVisible that paulmacd suggested and see how that works.  Thank you all very much for the help!
0
 
LVL 21

Expert Comment

by:Roy Cox
ID: 41820200
What I do is when I am creating my workbooks I colour the tabs red if I want to quickly hide or unhide them I use this code
Option Explicit
Option Private Module

Dim ws As Worksheet
Sub HideRed()
    For Each ws In ThisWorkbook.Worksheets
        If ws.Tab.Color = vbRed Then ws.visible = xlSheetVeryHidden
    Next ws
End Sub
Sub ShowRed()
    For Each ws In ThisWorkbook.Worksheets
        If ws.Tab.Color = vbRed Then ws.visible = xlSheetVisible
    Next ws
End Sub

Open in new window

1
 
LVL 14

Expert Comment

by:frankhelk
ID: 41880134
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: paulmacd (https:#a41820005)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

frankhelk
Experts-Exchange Cleanup Volunteer
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question