Macro - Return row of data to sheet2 from keyword on sheet 1

caudiffred

New Member
Joined
Feb 13, 2015
Messages
17
I have the below code that returns what I want. But it returns it to Column A. How do I get it to return the data starting in Column B?

Thank you,

Clint

Code:
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
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Change the two 10s to how many columns of data you want copied.

Code:
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
            [B]Sheets("Results").Range("B" & countRows2).Resize(,[COLOR="#FF0000"]10[/COLOR]).Value = Sheets("Data").Range("A" & j).Resize(,[COLOR="#FF0000"]10[/COLOR]).Value[/B]
            countRows2 = countRows2 + 1
        End If
    Next j
End Sub
 
Upvote 0
This script is copying the entire row.
You say you want to start copying from column B
We need to know from column b to column ?
Is it B to G:
Or B to P

???
 
Upvote 0
I am sorry, What I meant was, I still want it to copy the entire row, but when is inserts/pastes it onto sheet 2 I would like it to start inserting/pasting starting at Column B. With the current code, it inserts/pastes it into Row 6, Column A.
 
Last edited:
Upvote 0
Try this instead:
Rich (BB code):
Sub EFP()
    Dim keyword As String
    Dim countRows1 As Long, countRows2 As Long
    
    Dim rngData As Excel.Range
    Dim iColSize As Long
    
    keyword = Sheets("Results").Range("B3").Value
    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
            With Sheets("Data")
              Set rngData = Intersect(.Rows(j), .UsedRange)
            End With
            With rngData
              iColSize = .Columns.Count
              iRowSize = .Rows.Count
              Sheets("Results").Cells(countRows2, 2).Resize(iRowSize, iColSize).Value = .Value
            End With
            countRows2 = countRows2 + 1
        End If
    Next j
End Sub
 
Upvote 0
Not sure if this is the correct way or not, but I got it to do what I wanted with this.

Code:
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").Range("B" & countRows2 & ":Z" & countRows2).Value = Sheets("Data").Range("A" & j & ":Y" & j).Value
            countRows2 = countRows2 + 1
        End If
    Next j
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top