Copy Row to new sheet based on list comparison

skull_eagle

Board Regular
Joined
Mar 25, 2011
Messages
89
Hi All,

I have a list of names in column A in sheet 2. I need to check for this name (all or part of name) in column C of Sheet 1. If the name is found then copy the row of data from sheet 1 to a new sheet (the new sheet should be called the search name). Once sheet 1 has been checked for the name in A1 (sheet2) then it needs to be checked for the name in A2 (Sheet2) and these should be copied to a new sheet..etc etc etc until all names have been checked and there are 50 or so new sheets.

I'm not sure if it's easier to do it this way or to instead create all the new sheets from the list in column A (Sheet 2) and then delete the blank ones after.

I'm having some major trouble and any assistance would be a life saver, Thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try

Code:
Sub NameSearch()
Dim LR As Long, i As Long, Found As Boolean
Dim LR2 As Long, j As Long
LR2 = Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
With Sheets("Sheet2")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
        Found = False
        With .Range("A" & i)
            For j = 1 To LR2
                If InStr(.Value, Sheets("Sheet1").Range("C" & j).Value) > 0 Then
                    Found = True
                    Exit For
                End If
            Next j
            If Found Then
                Sheets.Add after:=Sheets(Sheets.Count)
                .EntireRow.Copy Destination:=ActiveSheet.Range("A1")
                On Error Resume Next
                ActiveSheet.Name = .Value
                On Error GoTo 0
            End If
        End With
    Next i
End With
End Sub
 
Upvote 0
Thank you so much for the code Peter.

A small problem,
I need it to copy over ever row that contains the name, the code appears to be only copying the first instance.
You also have ".EntireRow.Copy" in the code but it is only copying over the cell.

Any thoughts?


Thanks
 
Upvote 0
For me it is copying the entire row.

It will also copy every instance. However, only with the first instance will the sheet be named with the name (worksheet names cannot be the same).
 
Upvote 0
I tried a few different things, it appears it is copying the entire Row from sheet 2 instead of sheet 1, sheet 2 is simply a list of the names and contains no data, I need the data to be copied from sheet 1. Is there a quick tweak I can make to the code to change this.

Thanks for your help.
 
Upvote 0
Sorry, I thought Sheet2 was to be copied. Try

Code:
Sub NameSearch()
Dim LR As Long, i As Long, Found As Boolean
Dim LR2 As Long, j As Long
LR2 = Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
With Sheets("Sheet2")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
        Found = False
        With .Range("A" & i)
            For j = 1 To LR2
                If InStr(.Value, Sheets("Sheet1").Range("C" & j).Value) > 0 Then
                    Found = True
                    Exit For
                End If
            Next j
            If Found Then
                Sheets.Add after:=Sheets(Sheets.Count)
                Sheets("Sheet1").Range("C" & j).EntireRow.Copy Destination:=ActiveSheet.Range("A1")
                On Error Resume Next
                ActiveSheet.Name = .Value
                On Error GoTo 0
            End If
        End With
    Next i
End With
End Sub
 
Upvote 0
Thanks Peter, It is no copying from the data from sheet one.

I hate to be a pain but it is only copying over the first instance. 1 row per new tab, there should be several hundred....
 
Upvote 0
Try this

Code:
Sub NameSearch()
Dim LR As Long, i As Long
Dim LR2 As Long, j As Long
LR2 = Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
With Sheets("Sheet2")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
        With .Range("A" & i)
            For j = 1 To LR2
                If InStr(.Value, Sheets("Sheet1").Range("C" & j).Value) > 0 Then
                    Sheets.Add after:=Sheets(Sheets.Count)
                    Sheets("Sheet1").Range("C" & j).EntireRow.Copy Destination:=ActiveSheet.Range("A1")
                    On Error Resume Next
                    ActiveSheet.Name = .Value
                    On Error GoTo 0
                End If
            Next j
        End With
    Next i
End With
End Sub
 
Upvote 0
Not quite.

For instance in A1 on sheet 2 was "abc". It's called sheet 3 "abc" as it should, copied 1 row in there and then copied the next row for "abc" to sheet 4 (still called sheet 4), next row to sheet 5 (still called sheet 5) up to sheet 29.

Then sheet 30 is called "def" for the A2 search and same thing - 1 row on that sheet, sheet 31 contains the next row for "def" etc up to sheet 100

Sheet 101 is called "ghi" etc etc etc.
 
Upvote 0
Try

Code:
Sub NameSearch()
Dim LR As Long, i As Long
Dim LR2 As Long, j As Long
LR2 = Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
With Sheets("Sheet2")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
        With .Range("A" & i)
            For j = 1 To LR2
                If InStr(.Value, Sheets("Sheet1").Range("C" & j).Value) > 0 Then
                    If Not WorksheetExists(.Value) Then
                        Sheets.Add after:=Sheets(Sheets.Count)
                        On Error Resume Next
                        ActiveSheet.Name = .Value
                        On Error GoTo 0
                    End If
                    Sheets("Sheet1").Range("C" & j).EntireRow.Copy Destination:=ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1)
                End If
            Next j
        End With
    Next i
End With
End Sub


Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,220
Members
452,895
Latest member
BILLING GUY

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