Hi all.
I'm a decades long lurker here who's probably posted years ago but I don't recall my credentials from that time. Anyway, help would be appreciated.
I have the code for a search form (borrowed from Contextures and modified for my purposes). I can't seem to correctly reference the range RangeTrans for the AdvancedFilter. The code resides in the form.
Cheers.
I'm a decades long lurker here who's probably posted years ago but I don't recall my credentials from that time. Anyway, help would be appreciated.
I have the code for a search form (borrowed from Contextures and modified for my purposes). I can't seem to correctly reference the range RangeTrans for the AdvancedFilter. The code resides in the form.
Cheers.
VBA Code:
Option Explicit
Dim RangeTrans As Range
Dim RangeRes As Range
Dim lRowList As Long
Dim bEventsOff As Boolean
Dim wsPD As Worksheet
Dim wsSC As Worksheet
Dim wsSR As Worksheet
Sub ResultsListUpdate()
Dim RangeResStart As Range
Dim lRowSR As Long
Set wsPD = wsProjectData
Set wsSC = wsSearch
Set wsSR = wsSearchResult
Set RangeResStart = wsSearchResult.Range("A1")
Set RangeTrans = wsPD.Range("ProjDataHeader").CurrentRegion
Set RangeTrans = RangeTrans.Offset(1, 0).Resize(RangeTrans.Rows.Count - 1, RangeTrans.Columns.Count)
Application.ScreenUpdating = False
lstResults.ListIndex = -1
ClearSearchData
ClearSelectedTB
With wsSC.Range("CritSearch") _
.Range(wsSC.Cells(2, 1), wsSC.Cells(2, 3))
.ClearContents
.Value = Array(Me.txtSearch01.Value, Me.txtSearch02.Value, "")
End With
wsPD.Range("RangeTrans").AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=wsSC.Range("CritSearch"), _
CopyToRange:=RangeResStart, _
Unique:=False
lRowSR = 1
'Find last row in results list
lRowSR = wsSR.Cells.Find(What:="*", _
After:=RangeResStart, SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, _
LookIn:=xlValues).Row
If lRowSR = 1 Then
lstResults.RowSource = ""
Else
'Populate the form's list box (called lstResults) with the results.
Set RangeRes = RangeResStart.CurrentRegion
Set RangeRes = RangeRes.Offset(1, 0) _
.Resize(lRowSR - 1, RangeRes.Columns.Count)
ActiveWorkbook.Names.Add _
Name:="RangeRes", RefersTo:=RangeRes
lstResults.RowSource = "RangeRes"
End If
exitHandler:
Application.ScreenUpdating = True
Exit Sub
errHandler:
Resume exitHandler
End Sub
Last edited by a moderator: