简体   繁体   中英

Combo Box that searches 2 columns in a table and filters all rows that match either column

Please bear with me, I'm very new the VBA using excel though I have some experience using it for access. I am trying to create a combo box and using VBA I want that box to filter my table if the combo box selection matches either of the two columns. Basically I have a table of projects. each project has a lead and a secondary person assigned to it. I want to be able to select a name from the combo box (or if there is a better way to do it) and show all the projects that the name is associated with.

I have created a combo box with a targe range that is on a sepearte tab. Asside from that I could not figure out how to the autofilter funcition to work.

I believe I figured out how to accomplish this, though open to ideas on how to improve it. I created a hidden column that concatenates the 2 columns I want to filter against. I then created a listbox and 2 checkboxes to select the name I want to filter along with some combination of that name appearing in the first column, the 2nd, or either. I added the code to sub for any changes in the listbox or either checkbox.

Is there a way to select multiple names in the listbox so I can filter more than one person. I do see how I would run into problems in the concatenated column though.

Dim NameSelect As String

On Error Resume Next

NameSelect = NameList.Value

If PrimaryCheck = False And SecondaryCheck = False Then
    Sheets("Weekly Update").ListObjects("tblPipeline").AutoFilter.ShowAllData
    Exit Sub
ElseIf PrimaryCheck = True And SecondaryCheck = False Then
    Sheets("Weekly Update").ListObjects("tblPipeline").AutoFilter.ShowAllData
    Sheets("Weekly Update").ListObjects("tblPipeline").Range.AutoFilter Field:=11, Criteria1:="*" & NameSelect & "*"
    ActiveWindow.ScrollRow = 1
    Exit Sub
ElseIf PrimaryCheck = False And SecondaryCheck = True Then
    Sheets("Weekly Update").ListObjects("tblPipeline").AutoFilter.ShowAllData
    Sheets("Weekly Update").ListObjects("tblPipeline").Range.AutoFilter Field:=12, Criteria1:="*" & NameSelect & "*"
    ActiveWindow.ScrollRow = 1
    Exit Sub
ElseIf PrimaryCheck = True And SecondaryCheck = True Then
    Sheets("Weekly Update").ListObjects("tblPipeline").AutoFilter.ShowAllData
    Sheets("Weekly Update").ListObjects("tblPipeline").Range.AutoFilter Field:=13, Criteria1:="*" & NameSelect & "*"
    ActiveWindow.ScrollRow = 1
    Exit Sub
End If

On Error GoTo 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM