+ Reply to Thread
Results 1 to 5 of 5

Returns rows of matching columns

Hybrid View

  1. #1
    Registered User
    Join Date
    06-19-2009
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    3

    Returns rows of matching columns

    Hi,

    I'm looking for some code gurus to share some wisdom. Here's the situation:

    I have 2 worksheets and both contain Last Name and First Name columns. What I'm trying to do is run some VBA code that would

    > Check the Last and First name columns of sheet1
    > Find matching Last and First name columns values in sheet2
    > Return the entire rows of sheet2, for all matching values, in a new worksheet

    Hopefully, that's clear as mud. Basically, if I find an entry for Smith, John in worksheet1 and another Smith, John in worksheet2, I want to returnall the information in the Smith, John row for worksheet2 (in a newly created table). Many thanks in advance!!

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Returns rows of matching columns

    Sounds like you might be able to use the VLOOKUP formula. Attach a small sample workbook if not.

  3. #3
    Registered User
    Join Date
    06-19-2009
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Returns rows of matching columns

    Attached.......
    Attached Files Attached Files

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Returns rows of matching columns

    Hello biglifter,

    This macro has been added to the attaached workbook. There is button on the "Names" sheet to run the macro.
    Sub CopyNamesAndData()
    
      Dim Cell As Range
      Dim DSO As Object
      Dim Key As Variant
      Dim Keys As Variant
      Dim Item As Variant
      Dim R As Long
      Dim Rng As Range
      Dim RngEnd As Range
      Dim InfoWks As Worksheet
      Dim NamesWks As Worksheet
      Dim SummaryWks As Worksheet
      
        R = 2
        Set InfoWks = Worksheets("Info")
        Set NamesWks = Worksheets("Names")
        Set SummaryWks = Worksheets("Sheet3")
        
        Set Rng = InfoWks.Range("A2")
        Set RngEnd = Rng.Parent.Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Rng.Parent.Range(Rng, RngEnd))
        
        Set DSO = CreateObject("Scripting.Dictionary")
        DSO.CompareMode = vbTextCompare
        
          For Each Cell In Rng.Cells
            Key = Trim(Cell & Cell.Offset(0, 1))
            If Key <> "" Then
               If Not DSO.Exists(Key) Then
                  DSO.Add Key, Cell
               End If
            End If
          Next Cell
          
          Set Rng = NamesWks.Range("A2")
          Set RngEnd = Rng.Parent.Cells(Rows.Count, Rng.Column).End(xlUp)
          Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Rng.Parent.Range(Rng, RngEnd))
          
          For Each Cell In Rng.Cells
            Key = Trim(Cell & Cell.Offset(0, 1))
            If Key <> "" Then
               If DSO.Exists(Key) Then
                  DSO(Key).EntireRow.Copy SummaryWks.Cells(R, "A")
                  R = R + 1
               End If
            End If
          Next Cell
          
        Set DSO = Nothing
        
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    06-19-2009
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Returns rows of matching columns

    Thank you!!!! Worked perfectly
    Last edited by biglifter; 06-23-2009 at 10:52 AM.

+ 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