I want to copy rows from sheet1 only when the cell in column B has a specific value which is a string. I used the code below, which seems to copy the rows, but just pastes empty cells in the new sheet.
I think the problem is that it is looking for a value, not a string (can a string be a value?). Do I need to define the cell as a string? Sorry if these are silly questions - new to coding.
Sub copy_rows()
Sheets.Add After:=Sheets(1)
Sheets(2).Name = "New"
For Each cell In Sheets(1).Range("B:B")
If cell.Value = "banana" Then
matchRow = cell.Row
Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets("New").Select
ActiveSheet.Rows(matchRow).Select
ActiveSheet.Paste
End If
Next
End Sub
Here's an example of sheet 1:
apple 1 2 1
banana 1 3 5
carrot 1 1 1
banana 1 2 3
And here's the New sheet:
banana 1 3 5
banana 1 2 3
Also, I didn't attempt this in my code but it would be good if there were no space between the rows.
Thanks