Dear Team,
I had created the userform for Add, Update, Delete the customer details. And I am having listbox for display the details. When i am entering any values on Combobox2. it will search the customer name accordingly and display the details on Listbox1.
I want to update the header for my listbox1. The header range is Sheet "Customer Data" ("A2:H2").
My code is
Code
- Sub FindAll()
- 'For Filtering and Updating to Listbox1 by match the ComboBox2 Value from Sheet "Custmer Data"
- Dim strFind As String, rFilter As Range
- With Worksheets("Customer Data")
- Set rFilter = .Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp))
- Set rng = .Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp))
- End With
- With Worksheets("Customer Data")
- On Error Resume Next
- If Not .AutoFilterMode Then .Range("B2").AutoFilter
- On Error Resume Next
- End With
- strFind = Me.ComboBox2.Value
- rFilter.AutoFilter Field:=2, Criteria1:="=*" & strFind & "*", Operator:=xlFilterValues
- Set rng = rng.Cells.SpecialCells(xlCellTypeVisible)
- Me.ComboBox2.Clear
- For Each c In rng
- With Me.ListBox1
- .ColumnHeads = True
- .ColumnCount = 8
- .ColumnWidths = "30,150,100,110,80,100,75,125"
- .AddItem c.Value
- .List(.ListCount - 1, 1) = c.Offset(, 1)
- .List(.ListCount - 1, 2) = c.Offset(, 2)
- .List(.ListCount - 1, 3) = c.Offset(, 3)
- .List(.ListCount - 1, 4) = c.Offset(, 4)
- .List(.ListCount - 1, 5) = c.Offset(, 5)
- .List(.ListCount - 1, 6) = c.Offset(, 6)
- .List(.ListCount - 1, 7) = c.Offset(, 7)
- .List(.ListCount - 1, 8) = c.Offset(, 8)
- End With
- Next c
- Me.ListBox1.ListIndex = 0
- With Worksheets("Customer Data")
- .ShowAllData
- .AutoFilterMode = False
- End With
- End Sub
- Private Sub ComboBox2_Change()
- 'For search the values which is entering on ComboBox2
- Dim strFind As String, FirstAddress As String, rSearch As Range
- Dim f As Integer, lngItem As Long
- ListBox1.Clear
- With Worksheets("Customer Data")
- Set rSearch = .Range(.Cells(2, 2), .Cells(.Rows.Count, 2).End(xlUp))
- End With
- strFind = Me.ComboBox2.Value
- With rSearch
- Set c = .Find(strFind)
- If Not c Is Nothing Then
- With Me
- For lngItem = 1 To 8
- ' .Controls("ListBox1" & lngItem).Value = c.Offset(0, lngItem - 1).Value
- Next
- f = 0
- End With
- FirstAddress = c.Address
- Do
- f = f + 1
- Set c = .FindNext(c)
- Loop While Not c Is Nothing And c.Address <> FirstAddress
- If f > 0 Then
- Call FindAll
- End If
- Else
- ' MsgBox "Not Found. You entered '" & strFind & "' - Please Enter a valid First Name."
- End If
- End With
- With Worksheets("Customer Data")
- If .AutoFilterMode Then
- .AutoFilterMode = False
- End If
- End With
- End Sub
I don't know how to add this headers.
I am attaching my file here for your reference.
Please help me to solve this issue