MS Office Forum / Excel / New Users / December 2007
Compare and Highlight Rows
|
|
Thread rating:  |
Lisab - 11 Sep 2007 16:12 GMT I have an excel file with two worksheets. is there a way to programatically compare the two worksheets to find matches and highlight those matches on each worksheet.
They both have the same columns. I want to compare the InspectionID column. The Inspection ID column may have duplicates in both of the worksheets.
I have never done anything in excel above the beginner level. However, I am a very skilled VBA programmer (programmed in ACCESS for over 10 years).
Thank you
JE McGimpsey - 11 Sep 2007 16:23 GMT You may find it easier to use Conditional formatting instead:
For instance, with Sheet1 column A selected, and cell A1 active, checking against column B in sheet2:
Format/Conditional Formatting...
CF1: Formula Is =COUNTIF(Sheet2!B:B,A1) > 0 Format1: <pattern>/<color>
Do something similar with CF in Sheet2, column B.
> I have an excel file with two worksheets. is there a way to programatically > compare the two worksheets to find matches and highlight those matches on [quoted text clipped - 8 lines] > > Thank you Bernard Liengme - 11 Sep 2007 16:38 GMT Not sure this works. CF does not allow reference to other worksheets You could put the formula in a cell on Sheet 1 and use CF to look at its value. best wishes
 Signature Bernard V Liengme Microsoft Excel MVP www.stfx.ca/people/bliengme remove caps from email
> You may find it easier to use Conditional formatting instead: > [quoted text clipped - 22 lines] >> >> Thank you Lisab - 11 Sep 2007 16:48 GMT What do you think about this code. I think this may work if I knew the code for highlighting a row
Sub Find_MatchesINZips()
Dim compareRange As Variant Dim x As Variant, y As Variant
Set compareRange = Worksheets("Sheet2").Range("A2:A149")
For Each x In Selection For Each y In compareRange If x = y Then Highlighte Row ***(Need code here)*** Next y Next x
End Sub
> Not sure this works. CF does not allow reference to other worksheets > You could put the formula in a cell on Sheet 1 and use CF to look at its [quoted text clipped - 26 lines] > >> > >> Thank you JE McGimpsey - 11 Sep 2007 16:51 GMT Actually, I'm positive it doesn't, at least the way I posted. I tried to simplify, and forgot about the reference. Thanks for the correction, Bernard!
what DOES work is to NAME the columns in each sheet. For instance, name Column B in sheet2, say, "ID2"
Then in Sheet1, column A, use
CF1: Formula is =COUNTIF(ID2, A1) Format1: <pattern>/<red>
> Not sure this works. CF does not allow reference to other worksheets > You could put the formula in a cell on Sheet 1 and use CF to look at its [quoted text clipped - 11 lines] > > > > Do something similar with CF in Sheet2, column B. Dave Peterson - 11 Sep 2007 17:06 GMT Just a warning about the name ID2--it looks way too much like a cell address (when in A1 reference style).
How about _ID2 (with a leading underscore)
> Actually, I'm positive it doesn't, at least the way I posted. I tried to > simplify, and forgot about the reference. Thanks for the correction, [quoted text clipped - 29 lines] > > > > > > Do something similar with CF in Sheet2, column B.
 Signature Dave Peterson
JE McGimpsey - 11 Sep 2007 17:13 GMT OK, I'm officially too distracted to be posting here. I'm going back to just working...
Thanks for the correction, Dave.
> Just a warning about the name ID2--it looks way too much like a cell address > (when in A1 reference style). > > How about _ID2 > (with a leading underscore) Lisab - 11 Sep 2007 17:30 GMT Please, I am so close. I know this would work if I knew the syntax for accessing the current row and highlighting it.
Sub Find_MatchesINZips()
Dim compareRange As Variant Dim x As Variant, y As Variant
Set compareRange = Worksheets(2).Range("A2:A149")
For Each x In Selection For Each y In compareRange If x = y Then x.Rows.BackColor = vbYellow ****x.rows is not correct**** Next y Next x
End Sub
> OK, I'm officially too distracted to be posting here. I'm going back to > just working... [quoted text clipped - 6 lines] > > How about _ID2 > > (with a leading underscore) Peo Sjoblom - 11 Sep 2007 18:05 GMT Why swim the river to get to the water, you have been given a solution using conditional formatting?
 Signature Regards,
Peo Sjoblom
> Please, I am so close. I know this would work if I knew the syntax for > accessing the current row and highlighting it. [quoted text clipped - 26 lines] >> > How about _ID2 >> > (with a leading underscore) Lisab - 11 Sep 2007 19:00 GMT Because I know there is always more then one way to skin a cat. That is what makes me a great programmer. By the way, here is the solution. ------------------------------------ Dim compareRange As Variant Dim x As Variant, y As Variant Dim counter As Integer
Set compareRange = Worksheets(2).Range("A2:A149") counter = 1
For Each x In Selection For Each y In compareRange If x = y Then Selection.Rows(counter).EntireRow.Interior.ColorIndex = 6 Next y counter = counter + 1 Next x
> Why swim the river to get to the water, you have been given a solution using > conditional formatting? [quoted text clipped - 29 lines] > >> > How about _ID2 > >> > (with a leading underscore) JE McGimpsey - 11 Sep 2007 20:38 GMT Except that your solution is less efficient, non-automatic, and will not work for users who disable macros.
Given your original problem statement, it seems to me a real disadvantage that you rely on the inspectionID column on Worksheets(1) being selected rather than specifying it in your macro.
Your macro also doesn't meet your criterion of "highlight(ing) those matches on each worksheet", as it will only highlight the rows on sheet 1.
As a great programmer, you might also recognize that using variants is rather inefficient compared to using range objects or simple data types. And one should generally avoid using the Integer data type for rows, since Integers are limited to +32,767 and the number of rows isn't.
And, finally, the VBA comparison is likely much less efficient than using a built-in function, say:
Const idCol1 As Long = 1 'worksheet 1 ID column Const idCol2 As Long = 2 'worksheet 2 ID column Dim compareRange as Range Dim rCell As Range
With Worksheets(2) Set compareRange = .Range(.Cells(2, idCol2), _ .Cells(.Rows.Count, idCol2).End(xlUp).Row) End With With Worksheets(1) For Each rCell in .Range(.Cells(2, idCol1), _ .Cells(.Rows.Count, idCol1).End(xlUp).Row) With rCell If Application.WorksheetFunction.CountIf( _ compareRange, .Value) > 0 Then _ .EntireRow.Interior.ColorIndex = 6 End With Next rCell End With
> Because I know there is always more then one way to skin a cat. That is what > makes me a great programmer. By the way, here is the solution. [quoted text clipped - 17 lines] > > using > > conditional formatting? Lisab - 11 Sep 2007 18:04 GMT PLEASE HELP - Using the following code I am getting the following error
Unable to set the pattern property of the interior class
Sub Find_MatchesINZips()
Dim compareRange As Variant Dim x As Variant, y As Variant Dim counter As Integer
Set compareRange = Worksheets(2).Range("A2:A149") counter = 1
For Each x In Selection For Each y In compareRange If x = y Then Selection.Rows(counter).Interior.Pattern = vbYellow Next y counter = counter + 1 Next x
End Sub
> I have an excel file with two worksheets. is there a way to programatically > compare the two worksheets to find matches and highlight those matches on [quoted text clipped - 8 lines] > > Thank you Evgeny Grischenko - 06 Dec 2007 15:26 GMT Hello! please, try one useful add-in that i usually use for comparing spreadsheets: http://www.office-excel.com/excel-addins/compare-spreadsheets.html
Regards, Eugene
>I have an excel file with two worksheets. is there a way to >programatically [quoted text clipped - 10 lines] > > Thank you
|
|
|