Clint Audiffred
Clint Audiffred

Reputation: 23

VBA copy row from sheet1 to sheet2 based on keyword

My code does what I want, but it copies it to column A in sheet 2. I would like it to put the data in starting at Column B if possible.

Sub EFP()
Dim keyword As String: keyword = Sheets("Results").Range("B3").Value
Dim countRows1 As Long, countRows2 As Long
countRows1 = 3 'the first row of my dataset in the Data tab
endRows1 = 500 'the last row of my dataset in the Data tab
countRows2 = 6 'the first row where I want to start writing the found rows
For j = countRows1 To endRows1
    If Sheets("Data").Range("B" & j).Value = keyword Then
        Sheets("Results").Rows(countRows2).Value = Sheets("Data").Rows(j).Value
        countRows2 = countRows2 + 1
    End If
Next j

End Sub

Upvotes: 2

Views: 118

Answers (1)

teylyn
teylyn

Reputation: 35990

If you copy and paste whole rows, they will always start in column A. If you want the result to start in column B, you need a different approach, for example

Sheets("Results").Range("B" & countRows2 & ":Z" & countRows2).Value = Sheets("Data").Range("A" & j & ":Y" & j).Value

Upvotes: 2

Related Questions