+ Reply to Thread
Results 1 to 14 of 14

Identify if string exists, copy entire row.

Hybrid View

  1. #1
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Identify if string exists, copy entire row.

    Hey all,

    I am a bit stuck on how to go about this.

    I have a range of data on sheet1 that holds employees data, I need to constantly update this and add employees data as they are employed. I import a file that has all the recently added employees(Sheet 2) and I need to search through sheet 1 and see if that persons data is on sheet 2. If it exists on sheet 2 then move to the next employee. When I find an employee that has no no data on sheet1 , copy the entire row.

    Here is a sample WB.
    Attached Files Attached Files
    Last edited by JapanDave; 11-25-2011 at 11:07 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,449

    Re: Identify if string exists, copy entire row.

    Add a Helper Column and add this formula in row 2.

    =IF(COUNTIF(Sheet2!A:A,A2),"","copy")

    Copy down.

    Filter on the Helper Column.
    Select the visible rows and copy them
    Paste them into sheet 2

    You could record a macro while you do this to get the basic code but it would need generalising.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Identify if string exists, copy entire row.

    Hey Tm,

    I am not quite sure what I should be entering in the helper column?

  4. #4
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Identify if string exists, copy entire row.

    TM,

    I re-read your post and I think you may have what I am after the other way around.

    In reality sheet2 is supposed to represent a CSV file with employees data. I need to to get the added data into Sheet1 , not the other way around.

    This is part of a bigger piece of code where I am working between workbooks.

  5. #5
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Identify if string exists, copy entire row.

    Never mind , I see what you are saying now. I should be able to modify this to work for me. I will test and get back to the thread. For the time being I am going to leave this unsolved.

    @Pike, I will give that code a try as well.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,449

    Re: Identify if string exists, copy entire row.

    The formula ...

  7. #7
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Identify if string exists, copy entire row.

    will vba be ok .. you could try this old classic code
    option explicit
    Sub ptest()
        Dim ws1 As Worksheet, ws2 As Worksheet
        Dim lastRow As Long, i As Long, found As Range
        Application.ScreenUpdating = False
        With ThisWorkbook
            Set ws1 = .Sheets("Sheet1")
            Set ws2 = .Sheets("Sheet2")
    
        End With
        lastRow = ws1.Cells(Rows.Count, "a").End(xlUp).Row
        For i = 1 To ws1.Cells(Rows.Count, "a").End(xlUp).Row
           Set found = ws2.Range("a:a").Find(ws1.Cells(i, "a"), LookIn:=xlValues, LookAt:=xlWhole)
            If Not found Is Nothing Then
             ws1.Cells(i, "A").Resize(, 9).Copy Destination:=ws2.Cells(found.Row, "a")
    
            End If
        Next i
        Set ws2 = Nothing: Set ws1 = Nothing
        Set found = Nothing
        Application.ScreenUpdating = True
    End Sub
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,449

    Re: Identify if string exists, copy entire row.

    Re-reading your first post ...

    I need to search through sheet 1 and see if that persons data is on sheet 2

    Regards

  9. #9
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Identify if string exists, copy entire row.

    Man, I suck at explanations. Sorry for the confusion.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,449

    Re: Identify if string exists, copy entire row.

    No worries

  11. #11
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Identify if string exists, copy entire row.

    this updates sheet2 from sheet1 and if it not in sheet1 and no sheet 2 it adds it to sheet2 for
    Option Explicit
    Sub ptest()
        Dim ws1 As Worksheet, ws2 As Worksheet
        Dim lastRow As Long, i As Long, found As Range
        Application.ScreenUpdating = False
        With ThisWorkbook
            Set ws1 = .Sheets("Sheet1")
            Set ws2 = .Sheets("Sheet2")
        End With
        lastRow = ws1.Cells(Rows.Count, "a").End(xlUp).Row
        For i = 1 To ws1.Cells(Rows.Count, "a").End(xlUp).Row
            Set found = ws2.Range("a:a").Find(ws1.Cells(i, "a"), LookIn:=xlValues, LookAt:=xlWhole)
            If Not found Is Nothing Then
                ws1.Cells(i, "A").Resize(, 9).Copy Destination:=ws2.Cells(found.Row, "a")
            Else
                ws1.Cells(i, "A").Resize(, 9).Copy Destination:=ws2.Cells(ws2.Rows.Count, "a").End(xlUp).Offset(1, 0)
            End If
        Next i
        Set ws2 = Nothing: Set ws1 = Nothing
        Set found = Nothing
        Application.ScreenUpdating = True
    End Sub

  12. #12
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Identify if string exists, copy entire row.

    Hey Pike,

    I can't seem to get your code to work.

    I have attached a workbook.

    Never Mind, I re-read your code a found what "I" was doing wrong.
    Attached Files Attached Files
    Last edited by JapanDave; 11-25-2011 at 11:06 PM.

  13. #13
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Identify if string exists, copy entire row.

    alls good then dave .. what was wrong?

  14. #14
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Identify if string exists, copy entire row.

    I had not explained myself correctly. I needed to have the values pasted into sheet1. But I have rectified my mistake.

    Thanks for the concern.
    Dave

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1