'
'-----------
'FILTER CODE
'-----------
'
Private Sub txt_zoeken_Change()
' note: this assumes your listbox has 4 columns!
Dim lngLastRow As Long, lngRow As Long, lngCol As Long
Dim wks As Worksheet
Dim varData As Variant
Set wks = Workbooks("Klanten.xls").Sheets("Klant")
lst_klanten.Clear
If Len(txt_zoeken.text) > 0 Then
lngLastRow = wks.Cells(Rows.Count, "A").End(xlUp).row
' load data from sheet into array (faster than reading cells)
varData = wks.Range("B1:E" & lngLastRow).Value
' now loop through array
For lngRow = 2 To lngLastRow
For lngCol = 1 To 4
' check for text
If InStr(1, varData(lngRow, lngCol), txt_zoeken.text, vbTextCompare) > 0 Then
' found, so add data
With lst_klanten
.AddItem varData(lngRow, 1)
.List(.ListCount - 1, 1) = varData(lngRow, 2)
.List(.ListCount - 1, 2) = varData(lngRow, 3)
.List(.ListCount - 1, 3) = varData(lngRow, 4)
.List(.ListCount - 1, 3) = lngRow
End With
' text found, no need to check more columns
Exit For
End If
Next lngCol
' do next row of array
Next lngRow
Else
Call PopulateListClients
End If
End Sub
'
'------------
'POPULATELIST
'------------
'
Sub PopulateListClients()
Dim lbtarget As MSForms.ListBox
Dim rngSource As Range
Dim LastRow As Integer, n as Long
Dim varData
LastRow = Workbooks("Klanten.xls").ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).row
'Set reference to the range of data to be filled
Set rngSource = Workbooks("Klanten.xls").Worksheets("Klant").Range("B2:E" & LastRow)
varData = rngsource.Value
'Fill the listbox
Set lbtarget = Me.lst_klanten
With lbtarget
.Clear
'Determine number of columns
.ColumnCount = 5
'Set column widths
.Columnwidths = "75 pt;75 pt;75 pt;75 pt;0 pt"
'Insert the range of data supplied
for n = 1 to LastRow - 1
.AddItem vardata(n, 1)
.List(n - 1, 1) = varData(n, 2)
.List(n - 1, 2) = varData(n, 3)
.List(n - 1, 3) = varData(n, 4)
.List(n - 1, 4) = n + 1
next n
End With
End Sub
|