Solved

Compile error: Next without For

Posted on 2016-09-28
9
27 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

947 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

20 Experts available now in Live!

Get 1:1 Help Now