Hello.
Help me please with the following.
I want to create a macro which would search in two sheets all rows containing in the column H the searched value and return the results in another sheet (values in the column H are obtained using a formula - RIGHT($G2;5)).
For example: all searched data are located in the sheets "In" and "Out"
The result should be pasted in the sheet "Search"
Search parameter should be entered in cell B2 from the sheet "Search";
When inserting, for example, value 12558 in A2, I want that all rows from "In" and "Out", which contain in column H value 12558 to be pasted in the sheet "Search".
I have the below mentioned code but looks like it needs some adjustments (currently it searches by values located in column A from "In" and "Out" and returns wrong results.)
Can you please help me identify the error?
The Excel file is attached to this post.
Help me please with the following.
I want to create a macro which would search in two sheets all rows containing in the column H the searched value and return the results in another sheet (values in the column H are obtained using a formula - RIGHT($G2;5)).
For example: all searched data are located in the sheets "In" and "Out"
The result should be pasted in the sheet "Search"
Search parameter should be entered in cell B2 from the sheet "Search";
When inserting, for example, value 12558 in A2, I want that all rows from "In" and "Out", which contain in column H value 12558 to be pasted in the sheet "Search".
I have the below mentioned code but looks like it needs some adjustments (currently it searches by values located in column A from "In" and "Out" and returns wrong results.)
Can you please help me identify the error?
The Excel file is attached to this post.
Code:
Sub SearchCasesTest()Dim i As Long
Dim sval As String
Dim nextrow As Long
Dim inSheet As Worksheet
Dim outSheet As Worksheet
Dim searchSheet As Worksheet
Set inSheet = ThisWorkbook.Sheets("In")
Set outSheet = ThisWorkbook.Sheets("out")
Set searchSheet = ThisWorkbook.Sheets("Search")
sval = searchSheet.Range("a2").Value
searchSheet.Range("A5:K1000000").ClearContents
For i = 1 To inSheet.Range("A" & inSheet.Rows.Count).End(xlUp).Row
If inSheet.Cells(i, 1).Value = sval Then
nextrow = searchSheet.Range("A" & searchSheet.Rows.Count).End(xlUp).Row + 1
searchSheet.Range("A" & nextrow).Resize(, 11).Value = inSheet.Range("A" & nextrow).Resize(, 11).Value
End If
Next i
For i = 1 To outSheet.Range("A" & outSheet.Rows.Count).End(xlUp).Row
If outSheet.Cells(i, 1).Value = sval Then
nextrow = searchSheet.Range("A" & searchSheet.Rows.Count).End(xlUp).Row + 1
searchSheet.Range("A" & nextrow).Resize(, 11).Value = outSheet.Range("A" & nextrow).Resize(, 11).Value
End If
Next i
Sheets("Search").Range("A2").Select
End Sub