kelly mort
Well-known Member
- Joined
- Apr 10, 2017
- Messages
- 2,169
- Office Version
- 2016
- Platform
- Windows
I have this code here that I want to use to filter data and upload result to the listbox on a userform. So I have managed to add some pieces together and came closer, but I am left with how to trap certain errors.
The data starts on row 7. Row 6 is header – I don’t want to load header into the listbox.
I am having issues understanding what this line is doing actually.
When I change its values, 5 and 6, I see it decide which rows to start the load from. But since I have not been able to fully achieve what I want, I want to post it here so those with the capacity can have a look at it for me.
It also loads some empty rows to the listbox. Is it that of the “usedRange” causing that? If yes then I want to set up my own range like “.Range(“B7:X”&lr)”, where the lr is the last row. Is that a good idea?
Column H is where to look for the criteria from.
And since I am accepting the search criteria from an inputbox, it is possible I might not hit the right keyword at certain times. When this happen, I need to flag an alert. I am now adjusting to the filtering system and can’t seem to figure out where to pluck to make it happen – that’s when the criteria does not match anything in the field. Thanks for your time. have a nice moment.
The data starts on row 7. Row 6 is header – I don’t want to load header into the listbox.
I am having issues understanding what this line is doing actually.
Code:
Set rng = rng.Offset(5).Resize(rng.Rows.Count - 6, .ColumnCount)
When I change its values, 5 and 6, I see it decide which rows to start the load from. But since I have not been able to fully achieve what I want, I want to post it here so those with the capacity can have a look at it for me.
It also loads some empty rows to the listbox. Is it that of the “usedRange” causing that? If yes then I want to set up my own range like “.Range(“B7:X”&lr)”, where the lr is the last row. Is that a good idea?
Column H is where to look for the criteria from.
And since I am accepting the search criteria from an inputbox, it is possible I might not hit the right keyword at certain times. When this happen, I need to flag an alert. I am now adjusting to the filtering system and can’t seem to figure out where to pluck to make it happen – that’s when the criteria does not match anything in the field. Thanks for your time. have a nice moment.
Code:
Sub FilterData()
Dim rCrit As Range, rng As Range, r%, c%, LBox As Object
Set rng = Sheets(“Sheet1”).UsedRange
Set LBox = UserForm1.ListBox1
With Sheets(“Sheet1”)
.[B7].CurrentRegion.AutoFilter field:=7, Criteria1:=SearchItem
End With
With LBox.Object
While .ListCount > 0
.RemoveItem 0
Wend
Set rng = rng.Offset(5).Resize(rng.Rows.Count - 6, .ColumnCount)
Set rng = rng.SpecialCells(xlCellTypeVisible)
.List = rng.Value
For r = 0 To .ListCount - 1
For c = 0 To .ColumnCount - 1
.List(r, c) = Trim(rng.Cells(r + 1, c + 1).Text)
Next c
Next r
End With
If rng.Parent.FilterMode Then
rng.Parent.ShowAllData
End If
'rng.Parent.ShowAllData
End Sub