Consulting

Results 1 to 7 of 7

Thread: Match and copy.

  1. #1
    VBAX Newbie
    Joined
    Aug 2014
    Posts
    3
    Location

    Match and copy.

    Hi I have 2 sheets in a workbook, I want to compare column A in Sheet 1 with Column A in Sheet 2 (they contain company names) If there are any matches I want it to copy a value from the corresponding cell in column B Sheet 2 to a cell in sheet A also corresponding with the match.

    I can get this to work if I specify which value to search for but I need it to look for matches itself.

    Any help would be great its a bit beyond my ability.

  2. #2
    Which list would be longer, Sheet1 ColumnA or Sheet2 ColumnA? Would either list contain duplicate entries?

  3. #3
    VBAX Newbie
    Joined
    Aug 2014
    Posts
    3
    Location
    Hi Thanks the list in Sheet2 is longer. there will me no duplicate entries.
    Many Thanks

  4. #4
    Give this a try

    Sub test()
        Dim a As Long, b As Long, x As Long
        Dim aa As Range, bb As Range
        a = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
        b = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
        Set aa = Sheet1.Range("A1:A" & a)
        Set bb = Sheet2.Range("A1:A" & b)
        On Error Resume Next
            For Each Cell In bb
                x = aa.Find(Cell).Row
                Sheet1.Range("B" & x) = Cell.Offset(, 1)
            Next
        On Error GoTo 0
    End Sub

  5. #5
    Amended slightly

    Sub test()
        Dim a As Long, b As Long
        Dim aa As Range, bb As Range
        Application.ScreenUpdating = False
        a = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
        b = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
        Set aa = Sheet1.Range("A1:A" & a)
        Set bb = Sheet2.Range("A1:A" & b)
        On Error Resume Next
            For Each Cell In bb
                aa.Find(Cell).Offset(, 1) = Cell.Offset(, 1)
            Next
        On Error GoTo 0
        Application.ScreenUpdating = True
    End Sub

  6. #6
    VBAX Newbie
    Joined
    Aug 2014
    Posts
    3
    Location
    You my friend are a legend! Thank you very much

  7. #7
    You're welcome

Posting Permissions

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