Solved

Compile error: Next without For

Posted on 2016-09-28
9
29 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 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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 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 18

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

770 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