Pass listbox values to autofilter
Hi there,
I have some sample code which might help you get where you want to go. I
don't know exactly what part of the code you need and also I don't know
whether you want to pass multiple selections from the ListBox to one
individual column filter or multiple selections to a number of individual
column filters.
Therefore I am including sample code for setting the criteria of the filters
for both conditions plus sample code for extracting the selected data from
the MultiSelect ListBox. I'm making the assumption that you will then
probably be able to put code together to achieve your goal. However, if not
then I will need some more info on exactly what you want to do.
'Example 1:
'Setting criteria on multiple column filters
Dim numFilt1 As Single
Dim numFilt2 As Single
Dim strFilt1 As String
Dim strFilt2 As String
numFilt1 = 6 'Numeric value
numFilt2 = 10 'Numeric value
strFilt1 = Format(numFilt1, "0") 'Convert to string
strFilt2 = Format(numFilt2, "0") 'Convert to string
Selection.AutoFilter Field:=1, Criteria1:=strFilt1
Selection.AutoFilter Field:=2, Criteria1:=strFilt2
'Example 2:
'Setting custom multiple criteria on one filter with 'Or' condition
Dim numCrit1 As Single
Dim numCrit2 As Single
Dim strCrit1 As String
Dim strCrit2 As String
numCrit1 = 1 'Numeric value
numCrit2 = 5 'Numeric value
'Convert to string with = sign
strCrit1 = "=" & Format(numCrit1, "0")
strCrit2 = "=" & Format(numCrit2, "0")
Selection.AutoFilter Field:=1, Criteria1:=strCrit1, Operator:=xlOr, _
Criteria2:=strCrit2
'Example 3:
'Extracting the selected data from a MultiSelect listbox
'Note: List items start at zero hence 0 to ListCount-1
'You will need your form details in lieu of the sheet details
With Sheets("Sheet1").ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) = True Then
'this is where you make the criteria = the selections
MsgBox .List(i)
End If
Next i
End With
Regards,
OssieMac
"Ixtreme" wrote:
I have a form with some controls on it. One of them is a multi select
listbox from which the user can select 1, 2 or more values. How can I
pass the values to my autofilter in VBA?
I understand that this is not possible using comboboxes?
|