chipsworld
Board Regular
- Joined
- May 23, 2019
- Messages
- 161
- Office Version
- 365
Hi all...Need some guidance. I have been researching this all over, but I can't seem to find anything that is remotely the same..
I am trying to filter my list box based upon the value in one column (Only two options)
My list box is populated from a sheet that continues to grow, hence the .End(xlUp).Row) portion and not a fixed range of rows.
I have been trying to figre out how to apply the .Autofilter control, but am having zero luck so far...
As you can see, I am only displaying certain columns in the list box. There are two list boxes, one for a header and one for the data lines. The filter only needs to apply to "rng"
Any help would be greatly appreciated.
I am trying to filter my list box based upon the value in one column (Only two options)
My list box is populated from a sheet that continues to grow, hence the .End(xlUp).Row) portion and not a fixed range of rows.
I have been trying to figre out how to apply the .Autofilter control, but am having zero luck so far...
As you can see, I am only displaying certain columns in the list box. There are two list boxes, one for a header and one for the data lines. The filter only needs to apply to "rng"
Any help would be greatly appreciated.
VBA Code:
Dim ws As Worksheet
Dim rng As Range
Dim MyArray
Dim MyArray2 ' variant, receives one based 2-dim data field array
Dim rng2 As Range
Dim vData As Variant
Dim vTemp As Variant
Dim i As Long
Dim j As Long
Dim k As Long
Dim SortByCol As Long
SortByCol = 15
Set ws = Sheets("Historical") 'set sheetname
' Set range here
Set rng = ws.Range("A2:AF" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row) [B].AutoFilter(Field:=3, Criteria:="USAR") ' this part returns error "Compile Error: Named Argument not found"[/B]
Set rng2 = ws.Range("A1:AF1")
With Me.lstlabel
.Clear
.ColumnHeads = False
.ColumnCount = rng.Columns.Count
'create a one based 2-dim datafield array
MyArray = rng2
'fill listbox with array values
.List = MyArray
'Set the widths of the column here.
.ColumnWidths = "60;150;100;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;100;0;0;150;0;0"
.TopIndex = 0
End With
With Me.lstboxchoice
.Clear
.ColumnHeads = False
.ColumnCount = rng.Columns.Count
'create a one based 2-dim datafield array
MyArray2 = rng
'fill listbox with array values
.List = MyArray2
'Set the widths of the column here.
.ColumnWidths = "60;150;100;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;100;00;0;150;0;0"
.TopIndex = 0
End With
With Me.lstboxchoice
vData = .List
For i = LBound(vData, 1) To UBound(vData, 1) - 1
For j = i + 1 To UBound(vData, 1)
If vData(i, SortByCol) < vData(j, SortByCol) Then
For k = LBound(vData, 2) To UBound(vData, 2)
vTemp = vData(i, k)
vData(i, k) = vData(j, k)
vData(j, k) = vTemp
Next k
End If
Next j
Next i
.List = vData
End With