+ Reply to Thread
Results 1 to 3 of 3

Compare same Column in 2 Workbooks

  1. #1
    GregR
    Guest

    Compare same Column in 2 Workbooks

    I have 2 workbooks and want to compare Column A in both books. If there
    is no match for an item in WB1 Column A to WB2 Column A, I want to
    highlight or insert a cheater column in WB1 indicating "not in WB2".
    Similarly, if something is found in WB2 and not in WB1, highlight in a
    different color or "not in WB1". TIA

    Greg


  2. #2
    Forum Contributor kraljb's Avatar
    Join Date
    05-26-2004
    Location
    Illinois
    MS-Off Ver
    2007 (recent change)
    Posts
    256
    You don't need to write a macro to do this... you could do it with the VLookup function or the index and Match...

    i.e. insert column B of WB1 copy down for length of data in the column
    =if(type(Vlookup(a1,[WB2.xls]Sheet1!$A:$A,1,False))=16,"Not in WB2","")

    repeat for wb2 as well should should give you want...

    either that or a function like this...

    Sub CompareColumns()
    Dim wb1 as workbook
    dim wb2 as workbook
    dim ws1 as worksheet
    dim ws2 as worksheet
    dim rng1 as range
    dim rng2 as range
    dim c1 as range
    dim c2 as range
    dim bfound as boolean

    set wb1 = Workbooks("WB1")
    set wb2 = Workbooks("WB2")
    set ws1 = wb1.activesheet
    set ws2 = wb2.activesheet
    set rng1 = ws1.range(cells(1,1),cells(ws1.usedrange.rows.count, 1))
    Set rng2 = ws2.range(cells(1,1),cells(ws2.usedrange.rows.count, 1))

    for each c1 in rng1
    if c1 <> "" then
    bfound = false
    for each c2 in rng2
    if c1.value = c2.value then
    bfound = true
    end if
    next
    if not bfound then
    range(c1.offset(0,1).address).Value = "Not in WB2"
    end if
    end if
    next
    for each c2 in rng2
    if c2 <> "" then
    bfound = false
    for each c1 in rng1
    if c1.value = c2.value then
    bfound = true
    end if
    next
    if not bfound then
    range(c2.offset(0,1).address).value = "Not in WB1"
    end if
    end if
    next
    end sub

  3. #3
    GregR
    Guest

    Re: Compare same Column in 2 Workbooks

    Kraljb, thank for your formula, but when I try your sub, I get an error
    on the line set range:

    set rng1 =3D ws1.range(cells(1,1),cells(ws1=AD.usedrange.rows.count, 1))
    Set rng2 =3D ws2.range(cells(1,1),cells(ws2=AD.usedrange.rows.count, 1))

    Thanks

    Greg


+ 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