Home > Back-end >  Is there a way to filter VBA Userform's ListBox items by name?
Is there a way to filter VBA Userform's ListBox items by name?

Time:10-26

I have a data-entry Userform that sends data to different databases (worksheets). Since in the Excel file there are multiple non-database sheets i was wondering if it was possible to filter out and show only the worksheets that have in the name "databases" (useful so I don't have to modify the come each time i add a new database).

Can't upload images bc it's my first question,

Code:

Private Sub ListBox1_Click()

For i = 0 To UserForm1.ListBox1.ListCount - 1
    If UserForm1.ListBox1.Selected(i) = True Then
       Worksheets(UserForm1.ListBox1.List(i)).Activate
    End If
Next i

End Sub

Thank you.

CodePudding user response:

I hide worksheets that are not to be seen with a leading underscore. This allows me to loop all worksheets and easily hide them. You could use the same/similar approach to hide/show/activate the one sheet or sheets you want to have visible.

For Each ws In ThisWorkbook.Worksheets
 If Not ws.Name = Me.ListBox1.Caption Then
   ws.Visible = xlSheetVeryHidden
 ElseIf ws.Name = Me.ListBox1.Caption Then
   ws.Visible = xlSheetvisible
   ws.Activate
 End If
Next ws
  • Related