ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Comparing lists (https://www.excelbanter.com/excel-programming/299426-comparing-lists.html)

Sheeny[_11_]

Comparing lists
 
Hi,
Any help would be greatly appreciated.

I have two lists, old and new. I used the conditional formatting in
Excel to highlight and bold those in Old that do not appear in New, and
those in New that do not appear in old.

Now, I have two listboxes, one entitled Old and one entitled New. I
want to loop through cells A4:A135 and print those that are bold (
because they dont appear in New) in ListOld and similarly, want to loop
thorugh cells B4:B135 and print those that are bold in ListNew.

After coding this I found that it does not work for fields that are
conditionally bold. So I moved on to compare the lists, and print
those in Old that do not appear in New.

I came up with this:

Private Sub CmdRec_Click()

Dim i As Long

ListOld.Clear

Sheet3.Activate
Sheet3.Range("A4").Select

With Sheet3

For i = 1 To .Range("B" & Rows.Count).End(xlUp).Row
For j = 1 To .Range("A" & Rows.Count).End(xlUp).Row
If Sheet3.Cells(i, "B").Value < Sheet3.Cells(i,
"A").Value Then
ListOld.AddItem (Sheet3.Cells(i, "A").Value)
End If
Next j
Next i

End With

End Sub

Two problems with this:

1. It starts at A1 but I want it to start at A4
2. It prints the 'old' company NUMEROUS times, I guess everytime it
loops through the loop.

Any ideas on how to get what I want?

Thanks!!!
Sheeny


---
Message posted from http://www.ExcelForum.com/


kkknie[_104_]

Comparing lists
 
Here's some code based on your original:

Code
-------------------
Private Sub CmdRec_Click()

Dim i As Long
Dim j As Long
Dim bFound As Boolean

'Finds everything in column A that is not in column B
ListOld.Clear
Sheets("Sheet3").Select
Range("A4").Select

For i = 4 To Range("B65536").End(xlUp).Row
bFound = False
For j = 4 To Range("A65536").End(xlUp).Row
If Range("A" & i).Value = Range("B" & j).Value Then
bFound = True
Exit For
End If
Next
If bFound = False Then ListOld.AddItem Range("A" & i).Value
Next

'Finds everything in column B that is not in column A
ListNew.Clear
Sheets("Sheet3").Select
Range("A4").Select

For i = 4 To Range("B65536").End(xlUp).Row
bFound = False
For j = 4 To Range("A65536").End(xlUp).Row
If Range("A" & j).Value = Range("B" & i).Value Then
bFound = True
Exit For
End If
Next
If bFound = False Then ListNew.AddItem Range("B" & i).Value
Next


End Su
-------------------

I assumed both A and B ranges started in row 4 (thus the for i = 4 t
...). If this is wrong, change the 4 to wherever your data starts.



--
Message posted from http://www.ExcelForum.com


Sheeny[_12_]

Comparing lists
 
Thank you so much! That works wonderfully

--
Message posted from http://www.ExcelForum.com



All times are GMT +1. The time now is 02:11 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com