peterw1987
New Member
- Joined
- Nov 2, 2016
- Messages
- 17
https://drive.google.com/open?id=0B4xuRMTv-mCCc3ZnbFZwQWxUVTg
I have this userform(I named it payform) which I use to find/search data row, and then amend them. I copied and modified the code from other source.
But I just realize that the find button, find all the row if it match any of the 3 search criteria I set.
I want the find button to find the row if it match all of the 3 search criteria I set.
example: I want to find 1 A x row
1 A x
2 B y
1 B x
What the form is doing now is, it will show row 1(because it match 3 criteria) and row 3( because it match 2 criteria)
What I want is , it will only display the row 1.
Really need someone to teach me what to edit. Thanks before.
I have this userform(I named it payform) which I use to find/search data row, and then amend them. I copied and modified the code from other source.
But I just realize that the find button, find all the row if it match any of the 3 search criteria I set.
I want the find button to find the row if it match all of the 3 search criteria I set.
Code:
Private Sub buttSrch_Click() Dim shCurrent As Worksheet
Dim shResults As Worksheet
Dim found As Range
Dim firstFound As String
Dim SrchCol_1 As String
Dim SrchCol_2 As String
Dim SrchCol_3 As String
Dim r As String
If tbSrch1 = "" And tbSrch2 = "" And tbSrch3 = "" Then Exit Sub
Set shData = Sheets("Data") 'change to suit
Set rgData = shData.Cells.CurrentRegion
Set rgData = rgData.Offset(1, 0).Resize(rgData.Rows.Count - 1, rgData.Columns.Count)
Set shCurrent = ActiveSheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
Sheets("Results").Delete
On Error GoTo 0
Application.DisplayAlerts = True
Sheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.name = "Results"
Set shResults = Sheets("Results")
With shResults
.Cells(1, 1) = "DataRow"
.Cells(1, 5) = "HEADER1"
.Cells(1, 8) = "HEADER2"
.Cells(1, 13) = "HEADER3"
.Cells(1, 19) = "HEADER4"
.Cells(1, 22) = "HEADER5"
.Cells(1, 23) = "HEADER6"
.Cells(1, 24) = "HEADER7"
.Cells(1, 25) = "HEADER8"
.Cells(1, 26) = "HEADER9"
End With
'columns to search thru - change to suit
SrchCol_1 = "A"
SrchCol_2 = "D"
SrchCol_3 = "G"
lbResList.ListIndex = -1
tbResCol1 = ""
tbResCol2 = ""
tbResCol3 = ""
tbResCol4 = ""
tbResCol5 = ""
tbResCol6 = ""
tbResCol7 = ""
tbResCol8 = ""
tbResCol9 = ""
r = 1
If tbSrch1 <> "" Then
With rgData.Columns(SrchCol_1)
Set found = .Find(tbSrch1, rgData.Cells(rgData.Rows.Count, SrchCol_1))
If Not found Is Nothing Then
firstFound = found.Address
Do
r = r + 1
found.EntireRow.Copy shResults.Cells(r, 1)
shResults.Cells(r, 1).Insert Shift:=xlToRight
shResults.Cells(r, 1) = found.Row
Set found = .FindNext(found)
Loop While Not found Is Nothing And found.Address <> firstFound
End If
End With
End If
If tbSrch2 <> "" Then
With rgData.Columns(SrchCol_2)
Set found = .Find(tbSrch2, rgData.Cells(rgData.Rows.Count, SrchCol_2))
If Not found Is Nothing Then
firstFound = found.Address
Do
r = r + 1
found.EntireRow.Copy shResults.Cells(r, 1)
shResults.Cells(r, 1).Insert Shift:=xlToRight
shResults.Cells(r, 1) = found.Row
Set found = .FindNext(found)
Loop While Not found Is Nothing And found.Address <> firstFound
End If
End With
End If
If tbSrch3 <> "" Then
With rgData.Columns(SrchCol_3)
Set found = .Find(tbSrch3, rgData.Cells(rgData.Rows.Count, SrchCol_3))
If Not found Is Nothing Then
firstFound = found.Address
Do
r = r + 1
found.EntireRow.Copy shResults.Cells(r, 1)
shResults.Cells(r, 1).Insert Shift:=xlToRight
shResults.Cells(r, 1) = found.Row
Set found = .FindNext(found)
Loop While Not found Is Nothing And found.Address <> firstFound
End If
End With
End If
If r = 1 Then
lbResList.RowSource = ""
MsgBox "No Results"
Else
Set rgResults = shResults.Cells.CurrentRegion
Set rgResults = rgResults.Offset(1, 0).Resize(rgResults.Rows.Count - 1, 30)
rgResults.RemoveDuplicates Columns:=Array(1), Header:=xlNo
Set rgResults = shResults.Cells.CurrentRegion
Set rgResults = rgResults.Offset(1, 0).Resize(rgResults.Rows.Count - 1, 30)
ActiveWorkbook.Names.Add name:="rgResults", RefersTo:=rgResults
lbResList.RowSource = "rgResults"
End If
shCurrent.Activate
Application.ScreenUpdating = True
End Sub
example: I want to find 1 A x row
1 A x
2 B y
1 B x
What the form is doing now is, it will show row 1(because it match 3 criteria) and row 3( because it match 2 criteria)
What I want is , it will only display the row 1.
Really need someone to teach me what to edit. Thanks before.