Tell us more.
What range do you want to filter and what is the name of the listbox
And filter the values to where
And what column has this filter value?
Sub Filter_Me()
'Modified 11-26-17 9:45 PM EST
Application.ScreenUpdating = False
Dim ans As String
ans = ActiveSheet.ListBox1.Value
With ActiveSheet.Range(Cells(1, "C"), Cells(Cells(Rows.Count, "C").End(xlUp).Row, "C"))
.AutoFilter Field:=1, Criteria1:=ans
End With
Application.ScreenUpdating = True
End Sub
Assuming you want to filter Column "C"
And you will filter by the value in a Activex listbox named "ListBox1"
Try this script.
Now your Subject title said ListBox but then in your second post you said TextBox
So modify the script if needed.
Code:Sub Filter_Me() 'Modified 11-26-17 9:45 PM EST Application.ScreenUpdating = False Dim ans As String ans = ActiveSheet.ListBox1.Value With ActiveSheet.Range(Cells(1, "C"), Cells(Cells(Rows.Count, "C").End(xlUp).Row, "C")) .AutoFilter Field:=1, Criteria1:=ans End With Application.ScreenUpdating = True End Sub
@ bmrsalgas
Sharing a sample workbook rather than pictures would increase chances of getting the assistance you seek
Private Sub UserForm_Initialize()
FilterListBox1
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
FilterListBox1
End Sub
Private Sub FilterListBox1()
Dim r As Range
On Error Resume Next
With Worksheets("JobData")
Set r = .Range("A1:D" & .Cells(Rows.Count, "A").End(xlUp).Row)
r.AutoFilter 3, TextBox1
ListBox1.ColumnCount = r.Columns.Count
ListBox1.List = cellsToArray(r.SpecialCells(xlCellTypeVisible))
r.AutoFilter
End With
End Sub
Function cellsToArray(r As Range)
Dim a()
r.Copy
a = ClipboardToArray
cellsToArray = a
Application.CutCopyMode = False
End Function
Function ClipboardToArray()
Dim cb As Object 'Late Binding for MsForms.DataObject
'Dim cb As New MsForms.DataObject 'Early Binding
Dim s() As String, ss() As String, se As String, a()
Dim i As Long, j As Long
'Late Binding
Set cb = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
'On Error GoTo AnError
'Get data from the clipboard.
cb.GetFromClipboard
'Get clipboard contents
se = cb.GetText(1)
'Split se, delimiter vbcrlf
s() = Split(se, vbCrLf)
ss() = Split(s(0), vbTab)
ReDim a(LBound(s) To UBound(s), LBound(ss) To UBound(ss))
'~~> Split each strArray and store in strArray2, delimiter " ".
For i = LBound(s) To UBound(s)
ss = Split(s(i), vbTab)
For j = LBound(ss) To UBound(ss)
a(i, j) = ss(j)
Next j
Next i
ClipboardToArray = a()
Exit Function
AnError:
If Err <> 0 Then Debug.Print Err.Description
End Function