Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Compile error: Next without For

Posted on 2016-09-28
9
Medium Priority
?
39 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 35

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 35

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 35

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 9

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 22

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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.

580 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