KimberlyHeart
New Member
- Joined
- Mar 17, 2016
- Messages
- 19
<tbody>
[TD="class: postcell"] I have a spreadsheet with 1000+ rows of data, that I want to filter down based on two separate columns of values. I know I can do this using the AutoFilter option, but I am trying to do this using an Excel VBA script which prompts the user for the values it wants to filter on. So, I have a way for identifying the unique values available in each column I want to filter on:
Code:
Public Sub MakeFilter()
Dim Makes As Variant
Application.ScreenUpdating = False
'Get a temporary list of unique values from Make (column H)
ActiveSheet.Columns("H:H").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ActiveSheet.Range("EE1"), Unique:=True
'Sort the temporary list
ActiveSheet.Columns("EE:EE").Sort Key1:=ActiveSheet.Range("EE2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
'Put list into an array for looping
Makes = Application.WorksheetFunction.Transpose(ActiveSheet.Range("EE2:EE" & Rows.Count).SpecialCells(xlCellTypeConstants))
'clear temporary worksheet list
ActiveSheet.Range("EE:EE").Clear
D<code>im txt As String, k As Integer
For k = 1 To UBound(Makes)
txt = txt & Makes(k) & ", "
Next
MsgBox "Unique values:" & vbCrLf & txt
</code>End Sub
This works... and I get the message box with the values I want. So, my array "Makes" is holding the values I need.
I also have created a UserForm with a ListBox where I want to display these unique values and let the user multi-select which values they want to filter on.
How do I get the values from my previously created array to display in this UserForm/ListBox? I was trying this as the initial code inside the UserForm1:
Code:
Private Sub UserForm_Initialize()
Dim i As Integer
With ListBox1
For i = LBound(Makes) To UBound(Makes)
ListBox1.AddItem Makes(i)
Next i
End With
End Sub
But, for some reason, when I try to execute this code, via:
Code:
UserForm1.Show
which is the line immediately after the MsgBox line above, I get "ERROR 13 TYPE MISMATCH" with that UserForm1.Show line highlighted.
WHAT IS WRONG THERE?
Once the selections are made, how do I capture only those selected values and then apply the filter to my spreadsheet? Part of my code here is:
Code:
' Filter by Make
ActiveSheet.Range("A:N").AutoFilter Field:=8, Criteria1:=Array("FORD", "CHEVY", "DODGE", _
"NISSAN", "TOYOTA", "KIA", "LINCOLN", "CADILLAC", "MAZDA"), Operator:=xlFilterValues
But, that is with the hardcoded values.
How do I get the user selected values from the Makes array to be used in this filter?
[/TD]
</tbody>