Good Morning,
The attached macro has been very kindly modified by a forum representative to copy rows from the "Master sheet" to "Sheet2" when any cell in "Column B" of the "Master Sheet" is "like" a variable placed in cell G2 of the "Master Sheet" by the user. This Macro does the job extremely well but I would very much appreciate help in adding just one more selection criteria to the existing. I have made several attempts at adding the "and" function to the routine but to no avail. My knowledge of Macros can be written on a postage stamp.
Column "A" of the "Master Sheet" has a six digit numeric sequence starting at 10773 and currently ending at 20186 but this will increase as rows are added. I would like to also include column "A" within the existing selection criteria to only copy rows to "sheet2" if the cells within column "A" are between two values placed in say J2 and K2 of the master sheet. e.g. 10805 and 10926.
Hope I have explained this correctly.
Your help will be most appreciated
Take care
Trevor
Sub SearchForString()
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
On Error GoTo Err_Execute
'Start search in row 4
LSearchRow = 4
'Start copying data to row 10 in Sheet2 (row counter variable)
LCopyToRow = 10
While Len(Range("A" & CStr(LSearchRow)).Value) > 0
'If value in column B = "Value in G2", copy entire row to Sheet2
If Range("B" & CStr(LSearchRow)) Like "*" & Range("G2") & "*" Then
'Select row in Master to copy
Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
Selection.Copy
'Paste row into Sheet2 in next row
Sheets("Sheet2").Select
Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
'Move counter to next row
LCopyToRow = LCopyToRow + 1
'Go back to Master to continue searching
Sheets("Master").Select
End If
LSearchRow = LSearchRow + 1
Wend
'Position on cell A3
Application.CutCopyMode = False
Range("A3").Select
MsgBox "All matching data has been copied."
Exit Sub
Err_Execute:
MsgBox "An error occurred."
End Sub
Bookmarks