Solved

Compile error: Next without For

Posted on 2016-09-28
9
26 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
9 Comments
 
LVL 33

Expert Comment

by:paulmacd
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 33

Expert Comment

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

Adding that line resolves the compilation error though?
0
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 33

Accepted Solution

by:
paulmacd earned 500 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 17

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 13

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now