简体   繁体   中英

How to highlight duplicates rows based on more than 30 columns?

I am trying to highlight duplicate rows by Excel VBA code but I'm receiving a "'Range' of Object _Global' failed" error and I'm hoping someone can help as to why.

Sub Duplicate_Row()

Dim x As Integer
Dim Y As Integer
Dim Z As Integer

x = 2
Y = x + 1
Z = 2

Do While Range("A:AA" & Y) <> ""
Z = Z + 1

Do While Range("A:AA" & Y) <> ""

If Range("A:AA" & x) = Range("A:AA" & Y) And Range("A:AA" & Y).Interior.ColorIndex = xlColorIndexNone Then

Range("A:AA" & x).Activate

Selection.End(xlToLeft).Select

Range("A" & x).Select

Range(Selection, Selection.End(xlToRight)).Select
'Selection.Interior.Color = vbYellow
Selection.Interior.ColorIndex = Z

Range("A" & Y).Select
Range(Selection, Selection.End(xlToRight)).Select
'Selection.Interior.Color = vbYellow
Selection.Interior.ColorIndex = Z

End If

Y = Y + 1

Loop

x = x + 1
Y = x + 1

Loop

MsgBox "Done"

End Sub


How to highlight duplicates in rows with msgbox? I also want a msgbox for duplicate rows and highlight only those rows in which all the details are duplicate.

As my late father used to say " When you can do something the difficult way, then why do it the easy way? " and this is exactly what I see when reading your question. :-)

I have made a small Excel file, highlighting duplicates, using conditional formatting, as you can see in this screenshot:

在此处输入图像描述

(It's all about column "A", the others are just there for explanatory reasons: "B" contains the formula results, "C" contains the =FormulaText(Bn) formula.)

The formula this is based on, is: =COUNTIF($A$1:A1,A1)-1 .
Some explanation: this formula (you can see the results in the second column) counts the amount of times that An (which is what you get by dragging down A1 ) is present in the range A1:An (the first cell A1 is fixed because of the dollar-signs in the formula), minus one (in order not to count the cell value itself. As a result, when the value gets found before (a duplicate), the value gets 1 or higher, and when the value does not get found before, you get a zero. Conditional formatting can be based on formulae: TRUE or non-values get highlighted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM