How do I skip the header row?

matix003

New Member
Joined
Mar 18, 2022
Messages
21
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Insert this line: If cell.Row > 1 Then

Rich (BB code):
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)
      If cell.Row > 1 Then
        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
      End If
    Next cell
  Else
    ListBox4.Clear ' if no match then clear listbox
  End If
  ListBox4.SetFocus
End Sub
 
Upvote 0
Solution
Insert this line: If cell.Row > 1 Then

Rich (BB code):
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)
      If cell.Row > 1 Then
        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
      End If
    Next cell
  Else
    ListBox4.Clear ' if no match then clear listbox
  End If
  ListBox4.SetFocus
End Sub
Thank you! This works wonderfully!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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