+ Reply to Thread
Results 1 to 15 of 15

Search column in sheet to match data in another sheet issue

Hybrid View

mattyp Search column in sheet to... 01-22-2015, 04:26 PM
JOHN H. DAVIS Re: Search column in sheet to... 01-22-2015, 04:33 PM
mattyp Re: Search column in sheet to... 01-22-2015, 04:38 PM
JOHN H. DAVIS Re: Search column in sheet to... 01-22-2015, 04:50 PM
mattyp Re: Search column in sheet to... 01-22-2015, 04:54 PM
JOHN H. DAVIS Re: Search column in sheet to... 01-22-2015, 04:48 PM
mattyp Re: Search column in sheet to... 01-23-2015, 07:58 AM
JOHN H. DAVIS Re: Search column in sheet to... 01-23-2015, 08:42 AM
mattyp Re: Search column in sheet to... 01-23-2015, 08:44 AM
mattyp Re: Search column in sheet to... 01-23-2015, 09:01 AM
JOHN H. DAVIS Re: Search column in sheet to... 01-23-2015, 09:10 AM
mattyp Re: Search column in sheet to... 01-23-2015, 09:11 AM
JOHN H. DAVIS Re: Search column in sheet to... 01-23-2015, 09:25 AM
mattyp Re: Search column in sheet to... 01-23-2015, 09:29 AM
JOHN H. DAVIS Re: Search column in sheet to... 01-23-2015, 09:39 AM
  1. #1
    Registered User
    Join Date
    02-23-2012
    Location
    FL
    MS-Off Ver
    Excel 2007
    Posts
    55

    Search column in sheet to match data in another sheet issue

    So here's what I'm trying to do. Sheet2 has column A that contains data. If the data in Sheet2/Column A matches Sheet1/Column D, then copy that entire row to Sheet2/Column E. When it pastes into column E, it has to be in the saw row that was searched for originally. I can't attach the spreadsheet because it contains data that I just cannot share. If you need any more information, please let me know. This has got me completely stumped.



    sh1 = Sheets("Sheet1").Cells(Rows.Count, "D").End(xlUp).Row
    sh2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
    Sheets("Sheet2").Select
     
        For i = sh2 To 2 Step -1
            For j = sh1 To 2 Step -1
                If Sheets("Sheet1").Range("D" & j).Value = Sheets("Sheet2").Range("A" & i).Value Then
                    Sheets("Sheet1").Rows(j).Copy Destination:=Sheets("Sheet2").Range("E" & sh2 + 1)
                    sh2 = Sheets("Sheet2").Cells(Row.Count, "A").End(xlUp).Row
                End If
            Next j
        Next i

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Search column in sheet to match data in another sheet issue

    AFAIK you can't then copy that entire row to Sheet2/Column E. Only to Column A

  3. #3
    Registered User
    Join Date
    02-23-2012
    Location
    FL
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Search column in sheet to match data in another sheet issue

    Quote Originally Posted by JOHN H. DAVIS View Post
    AFAIK you can't then copy that entire row to Sheet2/Column E. Only to Column A
    Is there a work around that you know of? I really don't need the whole row copied from Sheet1, I just need all the cells in the row that contain data to be copied over.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Search column in sheet to match data in another sheet issue

    Or maybe:

    Sub mattyp()
    
    Sheets("Sheet1").Range(Cells(j, "D"), Cells(j, Sheets("Sheet1").UsedRange.Columns.Count)).Copy Destination:=Sheets("Sheet2").Range("E" & sh2 + 1)
    End Sub

  5. #5
    Registered User
    Join Date
    02-23-2012
    Location
    FL
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Search column in sheet to match data in another sheet issue

    Quote Originally Posted by JOHN H. DAVIS View Post
    Or maybe:

    Sub mattyp()
    
    Sheets("Sheet1").Range(Cells(j, "D"), Cells(j, Sheets("Sheet1").UsedRange.Columns.Count)).Copy Destination:=Sheets("Sheet2").Range("E" & sh2 + 1)
    End Sub
    I tried this but I'm getting a runtime 1004 error. "Application-defined or Object-defined error". It's pointing to the very first line of code - "sh1 = Sheets....." Any idea why it would throw that error?

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Search column in sheet to match data in another sheet issue

    You can copy a range:

    Sub mattyp()
    
    Sheets("Sheet1").Range(Cells(j, "D"), Cells(j, "YOUR LAST COLUMN TO COPY")).Copy Destination:=Sheets("Sheet2").Range("E" & sh2 + 1)
    End Sub

  7. #7
    Registered User
    Join Date
    02-23-2012
    Location
    FL
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Search column in sheet to match data in another sheet issue

    Anyone have any ideas?

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Search column in sheet to match data in another sheet issue

    Do you have Dim statements for sh1 and sh2 in your code?

  9. #9
    Registered User
    Join Date
    02-23-2012
    Location
    FL
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Search column in sheet to match data in another sheet issue

    Yes, they are both setup as Long. This really has me stumped because it looks like it should work.

  10. #10
    Registered User
    Join Date
    02-23-2012
    Location
    FL
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Search column in sheet to match data in another sheet issue

    Okay, I made some headway. I commented out a line and now I can step through the code with no error messages. It ALMOST works. ahahhaha

    What I mean is it will copy Sheet1/Row1 to Sheet2/Column E Row 10. But it won't copy any other rows over. Row 10 on Sheet 2 is where the last of the data is located, so it's started at the bottom of the data. This isn't a problem though as I really don't care if it starts at the top or bottom. I copied the code below exactly as it sits now.




    Sub Test()
    Dim sh1 As Long
    Dim sh2 As Long
     
    Sheets("Sheet2").Select
     
    sh1 = Sheets("Sheet1").Cells(Rows.Count, "D").End(xlUp).Row
    sh2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
     
     
        For i = sh2 To 2 Step -1
            For j = sh1 To 2 Step -1
                If Sheets("Sheet1").Range("D" & j).Value = Sheets("Sheet2").Range("A" & i).Value Then
                    Sheets("Sheet1").Activate
                    Sheets("Sheet1").Range(Cells(j, "A"), Cells(j, Sheets("Sheet1").UsedRange.Columns.Count)).Copy Destination:=Sheets("Sheet2").Range("E" & sh2)
                    'sh2 = Sheets("Sheet2").Cells(Row.Count, "A").End(xlUp).Row
                End If
                   
            Next j
        Next i
                   
     
    End Sub

  11. #11
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Search column in sheet to match data in another sheet issue

    Is there data in Sheet2 Column A?

  12. #12
    Registered User
    Join Date
    02-23-2012
    Location
    FL
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Search column in sheet to match data in another sheet issue

    Got it!!! I replaced the highlighted "sh2" with "i" and now the code runs through flawlessly. It runs from the bottom to the top. One last question though - How do I get the code to Start at Row 1 instead of at the bottom?


    Sheets("Sheet1").Range(Cells(j, "A"), Cells(j, Sheets("Sheet1").UsedRange.Columns.Count)).Copy Destination:=Sheets("Sheet2").Range("E" & sh2)

  13. #13
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Search column in sheet to match data in another sheet issue

    Just invert it

    
        For i = sh2 To 2 Step -1
        For i = 2 To sh2 
    Or j? Or both?

  14. #14
    Registered User
    Join Date
    02-23-2012
    Location
    FL
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Search column in sheet to match data in another sheet issue

    Just updating the i did exactly what I needed. Thanks again!!

  15. #15
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Search column in sheet to match data in another sheet issue

    You're welcome. Glad to help out.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 07-10-2013, 01:39 PM
  2. Replies: 9
    Last Post: 09-25-2012, 11:47 AM
  3. Replies: 4
    Last Post: 08-29-2012, 10:32 AM
  4. Replies: 2
    Last Post: 07-27-2012, 06:01 PM
  5. Match column from sheet 2 to sheet 1 and copy rows if match exists.
    By GravityInvert in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-28-2008, 01:42 PM

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