I have a macro set-up in a Excel workbook; its objective is: 1. In Sheet1, to search a table for a specific value in a column. 2. If that value is found, then it must copy the entire line to Sheet2.
Sub procurarnegociacion()
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
On Error GoTo Err_Execute
'Start search in row 5
LSearchRow = 5
'Start copying data to row 3 in Sheet2 (row counter variable)
LCopyToRow = 3
While Len(Range("A" & CStr(LSearchRow)).Value) > 0
'If value in column W = 1, copy entire row to Sheet2
If Range("W" & CStr(LSearchRow)).Value = "1" Then
'Select row in Sheet1 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 Sheet1 to continue searching
Sheets("Sheet1").Select
End If
LSearchRow = LSearchRow + 1
Wend
'Position on cell A3
Application.CutCopyMode = False
Range("A3").Select
MsgBox "All data copied."
Exit Sub
Err_Execute:
MsgBox "Error ocurred."
End Sub
But here is the problem: imagine I search for value "5" and I only have 1-5 values in that column W. The macro only works if I have all the 5's sorted out at the beginning of the table (descendent sort). If I have the W column in crescent sort, with all the 1's first, then it does not copy any line to Sheet2. It gives the message All data copied.", but then no line is copied on the Sheet2.
Why is that? Can you help me with this matter?