0

I am trying to search for text on a sheet in column c then if found within the same row select column a and copy and paste to sheet two. i have started with this code

Sub Test()
For Each Cell In Sheets("Asset Capture").Range("C35:C3000")
If Cell.Value = "MONITOR" Then
matchRow = Cell.Row
Rows.Range(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets("GRN Status Report").Select
lastRow = ActiveSheet.UsedRange.Rows.Count
If lastRow > 1 Then lastRow = lastRow + 1
ActiveSheet.Range("A" & lastRow).Select
ActiveSheet.Paste
Sheets("Asset Capture").Select
End If
Next
End Sub

but it is selecting the whole row and i can not figure out how to change the code to select data from just the A column?

jest3r
  • 1
  • 1

2 Answers2

0

Try this:

Sub Test()
    Dim Cell As Range, rngDest As Range

    Set rngDest = Sheets("Grn Status Report").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
    For Each Cell In Sheets("Asset Capture").Range("C35:C3000")
        If Cell.Value = "MONITOR" Then
            Cell.EntireRow.Cells(1).Copy rngDest
            Set rngDest = rngDest.Offset(1, 0)
        End If
    Next

End Sub

Note you don't need to use Select/Activate, and your code will be more robust if you avoid it as much as possible.

See: How to avoid using Select in Excel VBA macros

Community
  • 1
  • 1
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
0

Hope you looking for this

Sub Test()
increment = Worksheets("GRN Status Report").Range("A" & Rows.Count).End(xlUp).Row
For Each cell In Sheets("Asset Capture").Range("C5:C3000")
    If cell.Value = "MONITOR" Then
        matchrow = cell.Row
        matchcontent = Range("A" & matchrow).Value
        Worksheets("GRN Status Report").Cells(increment, 1) = matchcontent
        increment = increment + 1
    End If
Next
End Sub
Karthick Gunasekaran
  • 2,697
  • 1
  • 15
  • 25