1

Problem: I would like to have the ability to hide and unhide selected (multiple) sheets by a form

Available Resource: There many available resources that show how to unhide and hide all sheets at one but not flexible

Explain:

enter image description here

Because sheet5 is hidden checkbox corresponds to Sheet5 is checked.

Logic/approach:

  • Get all of the sheets' name include hided ones and bind them to Labels, check if it is hide or unhide and bind it to checkbox.

Label and Checkbox are created automatically, lable and checkbox is somehow linked to each other so the program knows which sheets to hide and to unhide.

  • write the code to check whether a checkbox changes its status between selected/unselected.
8
  • 1
    Also are you doing this in VB.Net/C#? Commented May 5, 2012 at 8:22
  • Thank you for asking. I am trying to do it in VBA of excel. The winform is just for explaining purpose: [1]I have learned how to hide and unhide sheet. [2] I have trouble with automatic create label and checkbox Commented May 5, 2012 at 9:36
  • 1
    Do you want to create all those controls at runtime? Or do you want to have those controls beforehand and simply enable/disable them based on whether the sheet is visible or not? Commented May 5, 2012 at 9:41
  • I would like to have the list when I run the form/macro. [1]The Form Display All Sheets (There are possible of 300sheets at max). [2]If the sheet is unhide make the checkbox is selected, if it is hide make the checkbox is unselected. [3]If I click on the selected ones - unhide, if I click on the unselected - hide. Commented May 5, 2012 at 9:51
  • 1
    There are possible of 300sheets at max In that case I suggest using a listbox or a combobox. :) Commented May 5, 2012 at 10:19

1 Answer 1

2

At last I am able to do the work. It is probably not good code, but it works.

enter image description here

Private Sub btListAllSheets_Click()

    With Me.ListBox1

    .Clear
    .ColumnHeads = True
    .ColumnCount = 2
    Dim status As String

    For i = 1 To Sheets.Count
        If Sheets(i).Visible = xlSheetHidden Then
           status = "Invisible"
        Else
           status = "Visible"
        End If

    ListBox1.AddItem (Sheets(i).Name)
    ListBox1.List(ListBox1.ListCount - 1, 1) = status

    Next i
    End With

End Sub

Private Sub bt_hideunhideselectedsheet_Click()

    Dim str As String
    str = Me.ListBox1.Column(1, Me.ListBox1.ListIndex)

    For Each Sh In ThisWorkbook.Worksheets

    If Sh.Name = Me.ListBox1.Value And str = "Visible" Then    
        Sh.Visible = False   
    ElseIf Sh.Name = Me.ListBox1.Value And str = "Invisible" Then    
        Sh.Visible = True
    End If

    Next Sh

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.