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
 
We could tell the script to never hide the first sheet in the workbook no matter what the name.

Would that work?
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
It would and perhaps you could post the entire code if someone else is looking for a great way to handle a lot of sheets in a workbook.

Thank you again for your code and your time!
 
Upvote 0
So are you saying all is working now.

You did not answer my question dealing with sheet named Start.

I can change script to always not hide first sheet in workbook no matter the name.

Do you need that or not?
 
Upvote 0
Yes, it would be nice to be able to use your code for the userform in other projects that I have!

So please include it!
 
Upvote 0
Try changing all your code to this:
Insures First sheet in workbook is not hidden no matter the name.

Code:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'Modified  12/18/2018  7:49:27 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  7:49:27 AM  EST
'Hide sheets
Dim ans As String
ans = Sheets(1).Name
If ListBox2.Value = ans Then MsgBox "Cannot hide sheet named  " & ans: 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  7:49:27 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  7:49:27 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  7:49:27 AM  EST
Dim i As Long
Dim ans As String
ans = Sheets(1).Name
ListBox2.Clear
ListBox3.Clear
For i = 1 To Sheets.Count
    If Sheets(i).Name <> ans Then Sheets(i).Visible = False: ListBox3.AddItem Sheets(i).Name
Next
ListBox2.AddItem ans
End Sub
Private Sub CommandButton2_Click()
'Modified  12/18/2018  7:49:27 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
Thank you again, now it is working really well! I prefer the version with Start in it since it works really well with my workbook.
 
Upvote 0
Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.
Thank you again, now it is working really well! I prefer the version with Start in it since it works really well with my workbook.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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