+ Reply to Thread
Results 1 to 3 of 3

VBA Compare & Match

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA Compare & Match

    Hi, I wonder whether someone may be able to help me pelase.

    From examples I've found, I'm trying to put together a short piece of code which performs the following:

    • Looks at column B on the "Flexible Resources List" sheet, then
    • Compare this to column D on the "All Data" sheet
    • If a match is found, copy the associated value in column C on the "Flexible Resources List" sheet, and
    • Paste into column O on the "All Data" sheet

    This is the code that I've put together so far:

    Sub Match()
    Dim Rng As Range, Dn As Range, n As Long
    Dim Dic As Object
    With Sheets("Flexible Resources List")
    Set Rng = .Range(.Range("B5"), .Range("B" & Rows.count).End(xlUp))
    End With
    Set Dic = CreateObject("scripting.dictionary")
    Dic.CompareMode = vbTextCompare
        For Each Dn In Rng
            Set Dic(Dn.Value) = Dn
        Next
    
    With Sheets("All Data")
        Set Rng = .Range(.Range("D5"), .Range("D" & Rows.count).End(xlUp))
    End With
    For Each Dn In Rng
        If Dic.exists(Dn.Value) Then
    
                 Dn.Offset(, 1) = Dic.Item(Dn.Value).Offset(, 0 + 1)
                 Dn.Offset(, 3) = Dic.Item(Dn.Value).Offset(, 3 + 10)
            
        End If
    Next Dn
    End Sub
    I can get the compare and match function working and the correct information is copied from the "Flexible Resources List" sheet, but the value pastes into column E, rather than column O on the "All data" sheet.

    I just wondered whether someone could possibly look at this please and let me know where I'm going wrong.

    Many thanks and kind regards

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: VBA Compare & Match

    Dn.Offset(, 1)
    will be one column to right of column D. So you must have
    Dn.Offset(, 11)
    for column O.

  3. #3
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Compare & Match

    Hi Izandol, thank you for taking the time to reply to my post and for helping me out. It now works perfectly.

    All the best and kind regards

+ 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: 6
    Last Post: 05-30-2012, 03:09 PM
  2. Compare/Match two columns
    By heartstealer in forum Excel General
    Replies: 3
    Last Post: 11-08-2010, 07:37 AM
  3. Compare 2 arrays using Match
    By skimmer333 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-07-2010, 03:38 AM
  4. Excel 2007 : question about compare and match
    By InfinitI in forum Excel General
    Replies: 5
    Last Post: 05-20-2009, 09:48 AM
  5. Can't use match to compare two columns
    By AC169 in forum Excel General
    Replies: 6
    Last Post: 04-16-2009, 04:58 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