I am filtering data in the listbox using values selected from two combo boxes. I have made a separate listbox for the headers. The code I have for filtering is also printing the headers. How do I stop the headers from getting printed along with the data?
VBA Code:
Private Sub FilterData()
Dim Manager As String 'Manager
Dim Lead As String 'Lead
Dim myDB As Range
With Me
If .cmb_BA_Manager.ListIndex < 0 Or .cmb_BA_Lead.ListIndex < 0 Then Exit Sub
Manager = .cmb_BA_Manager.Value 'aasign value from respective cmboboxes
Lead = .cmb_BA_Lead.Value
End With
With ActiveWorkbook.Sheets("EMPMaster")
Set myDB = .Range("A1:F1").Resize(.Cells(.Rows.Count, 1).End(xlUp).Row) 'Get data range of all the data
End With
With myDB
.AutoFilter 'remove filters
.AutoFilter Field:=3, Criteria1:=Manager 'filter data,field is column no of manager
.SpecialCells(xlCellTypeVisible).AutoFilter Field:=6, Criteria1:=Lead 'filter data again
Call UpdateListBox(Me.ListBox4, myDB, 1) 'last parameter is index of manager column
.AutoFilter
End With
End Sub
Sub UpdateListBox(ListBox4 As MSForms.ListBox, myDB As Range, columnToList As Long)
Dim cell As Range, dataValues As Range
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("EMPMaster")
If myDB.SpecialCells(xlCellTypeVisible).Count > myDB.Columns.Count Then
Set dataValues = myDB.Resize(myDB.Rows.Count)
ListBox4.Clear ' we clear the listbox before adding new elements
For Each cell In dataValues.Columns(columnToList).SpecialCells(xlCellTypeVisible)
With Me.ListBox4
.AddItem cell.Value
.List(.ListCount - 1, 1) = cell.Offset(0, 1).Value
.List(.ListCount - 1, 2) = cell.Offset(0, 2).Value
.List(.ListCount - 1, 3) = cell.Offset(0, 3).Value
.List(.ListCount - 1, 4) = cell.Offset(0, 4).Value
.List(.ListCount - 1, 5) = cell.Offset(0, 5).Value
.List(.ListCount - 1, 6) = cell.Offset(0, 6).Value
End With
Next cell
Else
ListBox4.Clear ' if no match then clear listbox
End If
ListBox4.SetFocus
End Sub