VBA autofilter restarting subroutine

RotherMen

New Member
Joined
Apr 18, 2018
Messages
6
About to throw my computer across the room. I've got a sub routine named 'Private Sub FullRosterListBox_Click()' that applies to a combobox on a user form. When the user selects a name from the combobox, i want to populate other fields on the user form with the appropriate data.

One of the items I'm trying to populate is another combobox with all the groups the person is assign too. I have 2 sheets, RosterTab and GroupsTab. I want to autofilter the RosterTab based on the Last Name and First Name the user clicked on.

I run the autofilter and after it applies the 1st filter, it immediately restarts the Subroutine from the beginning. No idea why this is happening. Here's the code:

Set RosterRange = RosterTab.Range(RosterTab.Cells(1, 1), RosterTab.Cells(Rows.Count, 3).End(xlUp))
With RosterRange
.AutoFilter Field:=1, Criteria1:=PersonLName
.AutoFilter Field:=2, Criteria1:=PersonFName
ActiveWorkbook.Names.Add Name:="PersonList", RefersTo:=RosterRange
End With

As soon as the first .AutoFilter line runs, it starts back over at the beginning of the subroutine. I've tried without the 'With RosterRange' too to no avail.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
How are you populating the Combobox?
 
Upvote 0
I'm actually trying to use that code to filter a spreadsheet, then create a named range to use as the source to populate the combobox.
 
Upvote 0
Well, I spoke too soon. Tried a couple different things... same result. After the first autofilter is applied, it restarts the sub routine. Full code below.
https://drive.google.com/file/d/1CsgfytmXQr0B0Wuh8Y7sTGhKHQ9cXZLm/view?usp=sharing
https://drive.google.com/drive/folders/1rkmD2w3j3pWlNMd1TyLUhP4LkY2afjxf?usp=sharing
Code:
Private Sub FullRosterListBox_Click()


    UpdateParticipant = True
 'I setup these 2 for testing purposes
    PersonLName = FullRosterListBox.List(FullRosterListBox.ListIndex, 0)
    PersonFName = FullRosterListBox.List(FullRosterListBox.ListIndex, 1)
    
[COLOR=#008000]    'Populates some fields on the user form based on the person selected in a combo box[/COLOR]
    Me.LastNameTextBox = FullRosterListBox.List(FullRosterListBox.ListIndex, 0)     'Last Name
    Me.FirstNameTextBox = FullRosterListBox.List(FullRosterListBox.ListIndex, 1)    'First Name
    Me.PhoneNumberTextBox = FullRosterListBox.List(FullRosterListBox.ListIndex, 3)  'Phone Number
    Me.EmailTextBox = FullRosterListBox.List(FullRosterListBox.ListIndex, 4)        'Email
    Me.NotesTextBox = FullRosterListBox.List(FullRosterListBox.ListIndex, 6)        'Notes
    
    If FullRosterListBox.List(FullRosterListBox.ListIndex, 5) <> "" Then            'Are they a leader
        Me.ManageParticipantsGroupLdrCkbox = True
    Else
        Me.ManageParticipantsGroupLdrCkbox = False
    End If
    
     If FullRosterListBox.List(FullRosterListBox.ListIndex, 7) <> "" Then           'Have they paid for current program
        Me.PaidUpCheckBox = True
    Else
        Me.PaidUpCheckBox = False
    End If
   
[COLOR=#008000]    'Preparing to load another combo box with all the groups the selected person is a participant[/COLOR]
    Set RosterRange = RosterTab.Range(RosterTab.Cells(1, 1), RosterTab.Cells(Rows.Count, 6).End(xlUp))
    RosterRange.Select
    
[COLOR=#ff0000]        'after it runs the 1st line below, it goes back to the start of the subroutine[/COLOR]
        Selection.AutoFilter Field:=1, Criteria1:=PersonLName, Operator:=xlAnd
        Selection.AutoFilter Field:=2, Criteria1:=PersonFName
 
Last edited by a moderator:
Upvote 0
Your 2nd link is locked & tells me I need permission.
 
Upvote 0
The links were just to .jpgs of the code. After more troubleshooting, I've discovered it's down to using the listbox selection as the information for the autofilter. All I had to do when the user clicked the name in the combo box was to set the variables I wanted to use to filter. The actual autofilter had to be moved out of the combo box click function. Once I did that. It worked fine.
 
Upvote 0

Forum statistics

Threads
1,214,879
Messages
6,122,065
Members
449,064
Latest member
scottdog129

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