Modify a toggle to incorporate an if statement..

sdoppke

Well-known Member
Joined
Jun 10, 2010
Messages
647
Hi everyone, i have a toggle to hide/unhide sheets, but sometimes those sheets are not in the worksheet and the user will not know it. Would anyone be able to help me mod this script to incorporate an if statement the will throw up a message box alerting the user?

Thanks a ton in advance :) heres the code:

Code:
Private Sub ToggleButton1_Click()
     'Toggles the To Do List sheet as hidden and unhidden
     Dim ws As Worksheet
    Application.ScreenUpdating = False
     
    For Each ws In Worksheets(Array("Week 1", "Week 2", "Week 3", "Week 4", "Week 5"))
        ws.Visible = Not ws.Visible
    Next ws
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi

Here's one possible way, which uses a function to determine whether the sheet exists or not.

HTH
DK

Code:
Private Sub ToggleButton1_Click()
'Toggles the To Do List sheet as hidden and unhidden
    Dim vSheetname As Variant
    Application.ScreenUpdating = False

    For Each vSheetname In Array("Week 1", "Week 2", "Week 3", "Week 4", "Week 5")

        If DoesSheetExist(CStr(vSheetname), ActiveWorkbook) Then
            ActiveWorkbook.Worksheets(vSheetname).Visible = Not ActiveWorkbook.Worksheets(vSheetname).Visible
        Else
            MsgBox "Worksheet " & vSheetname & " does not exist.", vbExclamation, "Error"
        End If

    Next vSheetname
    Application.ScreenUpdating = True
End Sub


Private Function DoesSheetExist(SheetName As String, wb As Workbook) As Boolean

On Error Resume Next

DoesSheetExist = CBool(Len(wb.Worksheets(SheetName).Name))

End Function
 
Upvote 0
Hi

Here's one possible way, which uses a function to determine whether the sheet exists or not.

HTH
DK

Code:
Private Sub ToggleButton1_Click()
'Toggles the To Do List sheet as hidden and unhidden
    Dim vSheetname As Variant
    Application.ScreenUpdating = False
 
    For Each vSheetname In Array("Week 1", "Week 2", "Week 3", "Week 4", "Week 5")
 
        If DoesSheetExist(CStr(vSheetname), ActiveWorkbook) Then
            ActiveWorkbook.Worksheets(vSheetname).Visible = Not ActiveWorkbook.Worksheets(vSheetname).Visible
        Else
            MsgBox "Worksheet " & vSheetname & " does not exist.", vbExclamation, "Error"
        End If
 
    Next vSheetname
    Application.ScreenUpdating = True
End Sub
 
 
Private Function DoesSheetExist(SheetName As String, wb As Workbook) As Boolean
 
On Error Resume Next
 
DoesSheetExist = CBool(Len(wb.Worksheets(SheetName).Name))
 
End Function


Tried it! Its great! You Rock!
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,283
Members
452,902
Latest member
Knuddeluff

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