rucker222

New Member
Joined
Mar 13, 2014
Messages
7
Hi guys,

I'm having trouble with some VBA I've put together. It basically just hides and Unhides worksheets which all works perfectly. The problem I'm having is with the message box.

"el" is always returning the next sheet in my mylist not the not the actual sheet which is not in my list...

For example if i change the name of sheet "B6 FFE" to "B FFE" the error message will say "Sheet B9 FFE Not Found!!!..." when i want it to say "Sheet B6 FFE Not Found!!!..."
Bad explanation but hopefully you will understand what I mean when you look at the below...

Code:
Private Sub Worksheet_Activate()
Application.ScreenUpdating = False

mylist = Array("Cover Sheet", "Summary", "FFE Equipment", "Fire & Smoke Doors", "B6 FFE", "B9 FFE", "B10 FFE", "B11 FFE", _
    "B12 FFE", "B13 FFE", "B14 FFE", "B16 FFE")
    
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name <> ActiveSheet.Name Then
ws.Visible = xlSheetVeryHidden
End If
Next ws


For Each el In mylist
If Err Then
      MSG1 = MsgBox("Sheet " & el & " Not Found!!" & vbNewLine & _
      el & " may have been deleted or renamed" & vbNewLine & _
      "would you like to unhide all worksheets?", vbYesNo + vbExclamation, "Sheet Not Found!")
      If MSG1 = vbYes Then
      Application.Run ("UnhideAllSheets")
      Exit Sub
    Else
    On Error Resume Next
      'MsgBox "E-mail successfully sent", vbInformation
    End If
    End If
    On Error Resume Next

    If Sheets(el).Visible = xlSheetVeryHidden Then
    Sheets(el).Visible = xlSheetVisible
    End If
    Next el
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
See if changes to your code helps:

Code:
Private Sub Worksheet_Activate()
    Dim el As Integer
    Dim ws As Worksheet
    
    mylist = Array("Cover Sheet", "Summary", "FFE Equipment", "Fire & Smoke Doors", "B6 FFE", "B9 FFE", "B10 FFE", "B11 FFE", _
                   "B12 FFE", "B13 FFE", "B14 FFE", "B16 FFE")


    
    Application.ScreenUpdating = False


    For Each ws In Worksheets
        If ws.Name <> ActiveSheet.Name Then
             ws.Visible = xlSheetVeryHidden
        End If
    Next ws


    On Error Resume Next
    For el = LBound(mylist) To UBound(mylist)
        Set ws = Sheets(mylist(el))
        If Err.Number = 9 Then
            MSG1 = MsgBox("Sheet " & mylist(el) & " Not Found!!" & vbNewLine & _
                          mylist(el) & " may have been deleted or renamed" & vbNewLine & _
                          "would you like to unhide all worksheets?", vbYesNo + vbExclamation, "Sheet Not Found!")
            If MSG1 = vbYes Then
                  Application.Run ("UnhideAllSheets")
                GoTo exitsub
            Else
                Sheets(mylist(el)).Visible = xlSheetVisible
                'MsgBox "E-mail successfully sent", vbInformation


            End If
        End If
        Set ws = Nothing
    Next el


exitsub:
    Application.ScreenUpdating = True
End Sub

Dave
 
Upvote 0
Hi Dave,

Thanks for the response. I gave that a go this morning and now it is correctly returning the error message box for the sheet that is missing/renamed. However it's also then returning the error message box for every subsequent sheet even though they are not missing/renamed.

Also all of the sheets in 'mylist' proceeding the missing/renamed sheet are not being made visible.

Any ideas?

Thanks again,
 
Upvote 0
see if this change helps:

Code:
Private Sub Worksheet_Activate()
    Dim el As Integer
    Dim ws As Worksheet
    mylist = Array("Cover Sheet", "Summary", "FFE Equipment", "Fire & Smoke Doors", "B6 FFE", "B9 FFE", "B10 FFE", "B11 FFE", _
                   "B12 FFE", "B13 FFE", "B14 FFE", "B16 FFE")

    Application.ScreenUpdating = False
    For Each ws In Worksheets
        If ws.Name <> ActiveSheet.Name Then
             ws.Visible = xlSheetVeryHidden
        End If
    Next ws
    On Error Resume Next
    For el = LBound(mylist) To UBound(mylist)
        Set ws = Sheets(mylist(el))
        If Err > 0 Then
            If Err.Number = 9 Then
                MSG1 = MsgBox("Sheet " & mylist(el) & " Not Found!!" & vbNewLine & _
                              mylist(el) & " may have been deleted or renamed" & vbNewLine & _
                              "would you like to unhide all worksheets?", vbYesNo + vbExclamation, "Sheet Not Found!")
                If MSG1 = vbYes Then
                     Application.Run ("UnhideAllSheets")
                    GoTo exitsub
                End If
                Err.Clear
            End If
        Else
            Sheets(mylist(el)).Visible = xlSheetVisible
            'MsgBox "E-mail successfully sent", vbInformation
        End If
        Set ws = Nothing
    Next el
exitsub:
    Application.ScreenUpdating = True
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,216,584
Messages
6,131,561
Members
449,655
Latest member
Anil K Sonawane

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top