Filter Listbox In Form

TellM1955

New Member
Joined
Apr 8, 2021
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
  2. Mobile
Hi all, once again I'm looking for some assistance. I've a created a form to add Students with daily defaults however, when a student is absent then I plan to will mark him as such. However, on the following day I need t to have a means of identifying those who are absent and change their status if necessary. To achieve this, I'm looking to filter those students who are identified as being absent to enable editing their records. I've attached a sample file to assist.

Thanks

OneDrive
 

Attachments

  • StudentForm-Filter.png
    StudentForm-Filter.png
    17.6 KB · Views: 10

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You are currently populating the list by loading all students from Sheet students using RowSource. You have to use a different method to filter it. In the Sub AddDataToListBox,
you will have to have a loop like this:
VBA Code:
   Dim Cell As Range
   Dim Col As Long
 
   With LstStudents
     
        For Each Cell In rg.Columns(1)

        .ColumnCount = rg.Columns.Count
        .ColumnWidths = "0;60;60;30;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0"

           If <this student is to be included> Then

              .AddItem Cell
              For Col = 2 To 29
                  .List(.ListCount - 1, Col - 1) = rg.Cells(.LstCount, Col)
              Next Col

         End If
    Next Cell
       
    
     End With
PENDING TESTING

This will also cause you to lose your column titles, and you will have to put the titles in a label on the UserForm above the listbox.

You didn't include enough information to explain how students are marked absent and how to use that for a particular day to exclude the student. So I can't provide the actual code to do the filtering. By the way, it would help to understand a little more about what you're doing. I am not clear on why, if a student is absent one day, you omit him from the list the following day.
 
Upvote 0
New and improved

VBA Code:
Private Sub AddDataToListBox()


    'Get the Range
    Dim rg As range
    Set rg = GetRange()
   
    'Link the data to the list box
    
   Dim Cell As range
   Dim Col As Long
  
   With LstStudents
  
      .ColumnCount = rg.Columns.Count
      .ColumnWidths = "0;60;60;30;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0"
     
      For Each Cell In rg.Columns(1).Cells

        
         If True Then ''''''''''''''''' your filter condition goes here
        
            .AddItem Cell
            For Col = 2 To 29
               .List(.ListCount - 1, Col - 1) = rg.Cells(.ListCount, Col).Value2
            Next Col
        
         End If
        
      Next Cell
  
  
   End With
    
txtFName.SetFocus
End Sub
 
Upvote 0
Many thanks for looking into this. I don't know what happened but it appears the wrong file was uploaded, this has been corrected and hopefully you can see how a student is marked absent. I've had a cursory look and changed out te code for AddDataToListBox1 but now get an error, to which a you'll probably surmise I have no idea as to why and what to do!
Cheers
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,612
Members
449,109
Latest member
Sebas8956

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