How can I hide row in listbox based on a condition?

matix003

New Member
Joined
Mar 18, 2022
Messages
21
Office Version
  1. 365
Platform
  1. Windows
I have some records being displayed in a listbox. I have a hide button in my userform which when clicked hides the row selected and the row remains in the excel sheet. But if I open the userform again then the data loaded has all the hidden rows. I have created a row 'Active' and assigned it true values. I want to change this value to false when the user hides that particular row. So that all the data in the excel sheet remains and only rows with 'Active' value True will be displayed in the listbox. How do I achieve this? Please help.
Here is my code -
VBA Code:
Sub Employee_Listbox()

    Dim sh As Worksheet
    Set sh = ThisWorkbook.Sheets("EMPMaster")
    
    Dim last_row As Long
    last_row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
    If last_row = 1 Then last_row = 2
    
    With Me.ListBox2
        .ColumnCount = 6
        .ColumnWidths = "150,70,100,50,70,0"
        .List = sh.Range("A2:F" & last_row).Value  '.RowSource = sh.Name & "!A2:F" & last_row
    End With
 
End Sub

Private Sub CommandButton15_Click() '''Hide button

If Me.ListBox2.ListIndex >= 0 Then
    Me.ListBox2.RemoveItem Me.ListBox2.ListIndex
    
End If

End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Seeing as how my vba strength is really in Access, I Googled "excel vba populate listbox based on criteria".
Wow, is it so much more complicated in Excel! Sorry I have no Excel code for this, otherwise I would have posted it. Maybe the search term clue will help you.
 
Upvote 0

Forum statistics

Threads
1,215,685
Messages
6,126,201
Members
449,298
Latest member
Jest

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