Compare Columns in Excel Issue

Pretty1996

Board Regular
Joined
Apr 28, 2010
Messages
100
Hi,
I’m hoping somebody can help me here,
I’ve been trying to get a piece of code to work that I saw on this site at the following link.
http://www.mrexcel.com/forum/showthread.php?t=330477
As you can see I posted my question there as well but got no reply.
The code supposedly does exactly what I need, however it doesn’t seem to work properly for me.
When running the code it will show me all the entries that are not in ‘Column A’, however it will only show me the first entry for a value that is not in ‘Column B’ and nothing else.
I’m using Excel 2007, not sure if that makes any difference.
Any help is much appreciated.
Regards
 
Hi Kobe,

Sorry I wasn't able to respond quicker. Here is what I was about to post, just for comparison. Glad you were able to work through it :-)

For data like:
Excel Workbook
B
1List 1
2chapelling
3crawdaddies
4crawdaddy
5crystalled
6crystalling
7daquiri
8expandibility
9expandible
10gospelled
11gospelling
12lother
13lothest
14orangoutan
15puky
16usurous
17wilfulnesses
18afficionado
19afficionados
20afterall
Sheet1
Excel 2010

And on another sheet:
Excel Workbook
F
1List 2
2bogieman
3borshch
4chapelled
5crawdaddies
6crawdaddy
7crystalled
8crystalling
9expandibility
10gospelled
11gospelling
12lother
13lothest
14onsides
15orangoutan
16puky
17wilfulnesses
18advertizer
19afficionado
20afficionados
21afterall
Sheet2
Excel 2010

In a Standard Module:
Rich (BB code):
Option Explicit
    
Sub example()
Dim DIC1            As Object
Dim DIC2            As Object
Dim rngLRow         As Range
Dim aryDataSheet1   As Variant
Dim aryDataSheet2   As Variant
Dim aryRet          As Variant
Dim n               As Long
Dim i               As Long
    
    Set DIC1 = CreateObject("Scripting.Dictionary")
    Set DIC2 = CreateObject("Scripting.Dictionary")
    
    With Sheet1 '<---Use CodeName or worksheet (tab) name --->  ThisWorkbook.Worksheets("Sheet1")
        '// Find the last cell in Col B with data and set a reference.                  //
        Set rngLRow = RangeFound(.Range(.Cells(2, "B"), .Cells(.Rows.Count, "B")))
        '// If we didn't find data, bailout...                                          //
        If rngLRow Is Nothing Then Exit Sub
        '// Plunk the vals into an array                                                //
        aryDataSheet1 = .Range(.Cells(2, "B"), rngLRow).Value
        '// Release the reference so that we can see if we found data in the next sheet //
        Set rngLRow = Nothing
    End With
    With Sheet2
        Set rngLRow = RangeFound(.Range(.Cells(2, "F"), .Cells(.Rows.Count, "F")))
        If rngLRow Is Nothing Then Exit Sub
        aryDataSheet2 = .Range(.Cells(2, "F"), rngLRow).Value
        'Set rngLRow = Nothing
    End With
    
    For n = 1 To UBound(aryDataSheet1, 1)
        '// Loop thru the array, skipping blanks, and add to the dictionary keys.       //
        '// If the key does not exist, it is created.  If it does exist, it's item is   //
        '// simply overwritten; thus, we get a list of unique values.                   //
        If Not aryDataSheet1(n, 1) = Empty Then DIC1.Item(aryDataSheet1(n, 1)) = Empty
    Next
    For n = 1 To UBound(aryDataSheet2, 1)
        If Not aryDataSheet2(n, 1) = Empty Then DIC2.Item(aryDataSheet2(n, 1)) = Empty
    Next
    
    '// Plunk both dictionary's keys into their respective arrays.                      //
    aryDataSheet1 = DIC1.Keys
    aryDataSheet2 = DIC2.Keys
    
    '// Oversize an output array to hold the missing vals from both lists.              //
    ReDim aryRet(1 To Application.Max(DIC1.Count, DIC2.Count), 1 To 2)
    
    '// Loop the missing items into the output array...                                 //
    For n = 0 To DIC1.Count - 1
        If Not DIC2.Exists(aryDataSheet1(n)) Then
            i = i + 1
            aryRet(i, 2) = aryDataSheet1(n)
        End If
    Next
    
    i = 0
    For n = 0 To DIC2.Count - 1
        If Not DIC1.Exists(aryDataSheet2(n)) Then
            i = i + 1
            aryRet(i, 1) = aryDataSheet2(n)
        End If
    Next
    
    '// ...and plunk them somewheres.                                                   //
    With ThisWorkbook.Worksheets.Add(After:=Sheet2, Type:=xlWorksheet)
        .Range("A1:B1").Value = Array("Missing in List1", "Missing in List2")
        .Range("A1:B1").Font.Bold = True
        .Range("A2").Resize(UBound(aryRet, 1), 2).Value = aryRet
        .Range("A1:B1").EntireColumn.AutoFit
    End With
End Sub
    
Function RangeFound(SearchRange As Range, _
                    Optional ByVal FindWhat As String = "*", _
                    Optional StartingAfter As Range, _
                    Optional LookAtTextOrFormula As XlFindLookIn = xlValues, _
                    Optional LookAtWholeOrPart As XlLookAt = xlPart, _
                    Optional SearchRowCol As XlSearchOrder = xlByRows, _
                    Optional SearchUpDn As XlSearchDirection = xlPrevious, _
                    Optional bMatchCase As Boolean = False) As Range
    
    If StartingAfter Is Nothing Then
        Set StartingAfter = SearchRange(1)
    End If
    
    Set RangeFound = SearchRange.Find(What:=FindWhat, _
                                      After:=StartingAfter, _
                                      LookIn:=LookAtTextOrFormula, _
                                      LookAt:=LookAtWholeOrPart, _
                                      SearchOrder:=SearchRowCol, _
                                      SearchDirection:=SearchUpDn, _
                                      MatchCase:=bMatchCase)
End Function

Mark
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi everyone! I've been trying to adapt the above VBA to fit my case but with no luck... I have a RANGE1 in Sheet A and a RANGE2 in Sheet B that I would like to compare and spit out the unique values in each. I've been working with this code, but it has a few drawbacks which I haven't been able to solve.

Code:
Sub CompareLists()Dim ListA As Range
Dim ListB As Range
Dim c As Range
Set ListA = Range("RANGE1")
Set ListB = Range("RANGE2")
Range("A1").Value = "Values in A that are NOT in B"
Range("B1").Value = "Values in B that are Not in A"
Range("C1").Value = "Count of A"
Range("D1").Value = "Count of B"
For Each c In ListA
If c.Value <> "" Then
    Range("C2").Value = Range("C2").Value + 1
    If Application.CountIf(ListB, c) = 0 Then
        Cells(Cells(Rows.Count, "A").End(xlUp).Row + 1, "A").Value = c
    End If
End If
Next c
For Each c In ListB
If c.Value <> "" Then
    Range("D2").Value = Range("D2").Value + 1
    If Application.CountIf(ListA, c) = 0 Then
        Cells(Cells(Rows.Count, "B").End(xlUp).Row + 1, "B").Value = c
    End If
End If
Next c
End Sub

However, I need this to only show unique values in each. For example, if RANGE1 is 1, 1, 3 and RANGE2 is 3, 4, 5 it will spit out 1 and 1, instead of only 1. Does anyone know how I could make it do this? Also, would it be possible to remove the previous data (below the headers) every time that the macro is run? Otherwise it adds data under the previous data and just continues to grow.

I'd very much appreciate your help!
 
Upvote 0
Hi everyone! I've been trying to adapt the above VBA to fit my case but with no luck... I have a RANGE1 in Sheet A and a RANGE2 in Sheet B that I would like to compare and spit out the unique values in each. I've been working with this code, but it has a few drawbacks which I haven't been able to solve.

...

However, I need this to only show unique values in each. For example, if RANGE1 is 1, 1, 3 and RANGE2 is 3, 4, 5 it will spit out 1 and 1, instead of only 1. Does anyone know how I could make it do this? Also, would it be possible to remove the previous data (below the headers) every time that the macro is run? Otherwise it adds data under the previous data and just continues to grow.

I'd very much appreciate your help!
would this thread be of any interest to you? http://www.mrexcel.com/forum/showth...ing-two-columns-of-Data&p=3195906#post3195906
 
Upvote 0
Upvote 0
Hi everyone! I've been trying to adapt the above VBA to fit my case but with no luck... I have a RANGE1 in Sheet A and a RANGE2 in Sheet B that I would like to compare and spit out the unique values in each. I've been working with this code, but it has a few drawbacks which I haven't been able to solve.

.... For example, if RANGE1 is 1, 1, 3 and RANGE2 is 3, 4, 5 it will spit out 1 and 1, instead of only 1.

At least for me, I am not understanding the logic. Could you provide some sample data for each range, what the expected outcome would be, and why we would get the outcome?

Mark
 
Upvote 0

Forum statistics

Threads
1,225,278
Messages
6,184,029
Members
453,206
Latest member
Atko

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top