Using two listboxes to show or hide worksheets

Waimea

Active Member
Joined
Jun 30, 2018
Messages
465
Office Version
  1. 365
Platform
  1. Windows
I found this piece of code https://www.ozgrid.com/forum/forum/...-worksheet-based-on-selection-made-on-listbox that hides or shows spreadsheets, I want to change this code to have two list boxes and two additional buttons to hide or show worksheets and when I transfer a sheet from listbox1 to listbox2 I want it to be able to hide with xlVeryHidden.

Code:
Option Explicit


Public CurrentBook As Workbook
Dim DisableMyEvents As Boolean


Private Sub CommandButton1_Click()
    Dim i As Long
    DisableMyEvents = True
    With ListBox1
        For i = 0 To .ListCount - 1
            .Selected(i) = False
        Next i
    End With
    DisableMyEvents = False
    Call ListBox1_Change
End Sub


Private Sub CommandButton2_Click()
    Dim i As Long
    DisableMyEvents = True
    With ListBox1
        For i = 0 To .ListCount - 1
            .Selected(i) = True
        Next i
    End With
    DisableMyEvents = False
    Call ListBox1_Change
End Sub


Private Sub CommandButton3_Click()
    Unload Me
End Sub


Private Sub ListBox1_Change()
    If DisableMyEvents Then Exit Sub
    Dim i As Long
    Application.ScreenUpdating = False
    With ListBox1
        For i = 0 To .ListCount - 1
            IndicatedSheet(i).Visible = IIf(.Selected(i), xlSheetHidden, xlSheetVisible)
        Next i
    End With
    Application.ScreenUpdating = True
End Sub


Function IndicatedSheet(Index As Long) As Worksheet
    If -1 < Index And Index < ListBox1.ListCount Then
        Set IndicatedSheet = CurrentBook.Sheets(ListBox1.List(Index, 0))
    End If
End Function


Private Sub UserForm_Activate()
    Dim i As Long
    Dim N As Long


    DisableMyEvents = True
    
    For N = 3 To CurrentBook.Sheets.Count
        ListBox1.AddItem ActiveWorkbook.Sheets(N).Name
    Next N
    
    With ListBox1
        For i = 0 To .ListCount - 1
            .Selected(i) = Not (IndicatedSheet(i).Visible = xlSheetVisible)
        Next i
    End With
    DisableMyEvents = False
End Sub


Private Sub UserForm_Initialize()
    With ListBox1: Rem these properties can be Set at design time
        .ColumnCount = 2
        .ColumnWidths = ";0"
        .BackColor = Me.BackColor
        .ListStyle = fmListStyleOption
        .MultiSelect = fmMultiSelectMulti
        .SpecialEffect = fmSpecialEffectFlat
    End With
    
    Set CurrentBook = ThisWorkbook
End Sub
 
Just test the parts I provided.

Do not use Show all sheets and Hide all sheets at this time.

Does all the other parts work?
 
Last edited:
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Ok, I understand what you want me to do. The 3 listboxes does what I want but when I click on the last sheet in listbox2 I get an error message because atleast one sheets has to be visible?
 
Upvote 0
Actually Show all an hide all work but do not update Listbox2 and Listbox3

Correct.
 
Upvote 0
Try these two new script for hide and show all sheets.
These will update Both list boxes.

Code:
Private Sub CommandButton1_Click()
'Hide All Sheets
'Modified  12/18/2018  6:03:19 AM  EST
Dim i As Long
ListBox2.Clear
ListBox3.Clear
For i = 1 To Sheets.Count
    If Sheets(i).Name <> "Start" Then Sheets(i).Visible = False: ListBox3.AddItem Sheets(i).Name
Next
End Sub
Private Sub CommandButton2_Click()
'Modified  12/18/2018  6:03:19 AM  EST
'Show all sheets
Dim i As Long
ListBox2.Clear
ListBox3.Clear
For i = 1 To Sheets.Count
    Sheets(i).Visible = True
    ListBox2.AddItem Sheets(i).Name
Next
End Sub
 
Upvote 0
Listbox1 works as expected!

Show all and hide all updates the listbox2 and listbox3 but doesn't hide or show the sheets when I click on a sheet in listbox3?

Listbox2 still crashes when there is only one sheet in it and I click that sheet.
 
Upvote 0
Well you get a crash if you hide all sheets. You cannot hide all sheets.
I'm working on that.
I will tell it to never hide sheet named Start.

I will get back with you on both issues.
 
Upvote 0
Try this:

Code:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'Modified  12/18/2018  6:46:58 AM  EST
'Listbox1 Goto Sheet
If Sheets(ListBox1.Value).Visible = True Then
    Application.Goto Sheets(ListBox1.Value).Range("A1")
Else
MsgBox "Sheets  " & ListBox1.Value & "  Is Hidden and we cannot go there"
End If
End Sub
Private Sub ListBox2_Click()
'Modified  12/18/2018  6:46:58 AM  EST
'Hide sheets
If ListBox2.Value = "Start" Then MsgBox "Cannot hide sheet named Start": Exit Sub
Sheets(ListBox2.Value).Visible = False
Dim i As Long
ListBox3.AddItem ListBox2.Value
ListBox2.Clear
For i = 1 To Sheets.Count
If Sheets(i).Visible = True Then ListBox2.AddItem Sheets(i).Name
Next
End Sub
Private Sub ListBox3_Click()
'Modified  12/18/2018  6:46:58 AM  EST
'Show sheets
Sheets(ListBox3.Value).Visible = True
Dim i As Long
ListBox2.AddItem ListBox3.Value
ListBox3.Clear
For i = 1 To Sheets.Count
If Sheets(i).Visible = False Then ListBox3.AddItem Sheets(i).Name
Next
End Sub
Private Sub UserForm_Initialize()
'Modified  12/18/2018  6:46:58 AM  EST
Dim i As Long
For i = 1 To Sheets.Count
    ListBox1.AddItem Sheets(i).Name
    If Sheets(i).Visible = True Then ListBox2.AddItem Sheets(i).Name
    If Sheets(i).Visible = False Then ListBox3.AddItem Sheets(i).Name
    
Next
End Sub
Private Sub CommandButton1_Click()
'Hide All Sheets
'Modified  12/18/2018  6:03:19 AM  EST
Dim i As Long
ListBox2.Clear
ListBox3.Clear
For i = 1 To Sheets.Count
    If Sheets(i).Name <> "Start" Then Sheets(i).Visible = False: ListBox3.AddItem Sheets(i).Name
Next
ListBox2.AddItem "Start"
End Sub
Private Sub CommandButton2_Click()
'Modified  12/18/2018  6:03:19 AM  EST
'Show all sheets
Dim i As Long
ListBox2.Clear
ListBox3.Clear
For i = 1 To Sheets.Count
    Sheets(i).Visible = True
    ListBox2.AddItem Sheets(i).Name
Next
End Sub
 
Upvote 0
Now it works great! I did however test it with another spreadsheet without the sheet "Start" and then listbox3 doesn't work.
 
Upvote 0
Well you original said hide all sheets but "Start" so then what sheet do we not want to hide.
Excel does not allow all sheets to be hidden.

And what do you mean does not work?

Do you mean when you click on any value in listbox3 nothing happens?

And when you say another
spreadsheet
. Do you mean another Workbook with the same Userform.

Excel does not use the term Spreadsheet any more. That is a term from the 1970's

We now use Workbook and Worksheet.
 
Upvote 0
Thank you for your hard work, it works great in my wookbook with a worksheet named "Start".

In a empty workbook with the same userform with worksheets Sheet1, Sheet2 and Sheet3 it crashes when I try to hide all worksheets, because atleast one worksheet has to be invisble I think. And without the worksheet Start it crashes as listbox2.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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