迭代谷底范围并检查是否重复

时间:2019-07-05 12:04:35

标签: excel vba

我正在遍历某个范围,并希望突出显示重复的范围。

我试图编写一个子程序,以检查范围内的任何值是否重复自身,如果重复,我想突出显示它

稍后,我想检查该值是否重复自身超过x次(但这很简单,只需添加一个计数器并将其递增)

我尝试使用两种不同的方法

方法1

Sub Highlight()

Dim count, countMax As Integer
countMax = 100
Dim rng As range, cell As range
Set rng = range("G1:G100")

For Each cell In rng

    For count = 1 To countMax
        Selection.Offset(1, 0).Select

        If Not IsEmpty(cell) And cell.Value = Selection.Value Then

            cell.Interior.color = RGB(0, 255, 0)

        End If
        countMax = countMax - 1
    Next count
Next cell

End Sub

方法2

Sub Highlight()

Dim rngG1 As range, cellG1 As range
Set rngG1 = range("G1:G100")

Dim rngG2 As range, cellG2 As range
Set rngG2 = range("G1:G100")

For Each cellG1 In rngG1

    For Each cellG2 In rngG2
        If cellG1 <> cellG2 And Not IsEmpty(cellG1) And cellG1.Value = cellG2.Value Then
            cellG2.Interior.color = RGB(0, 255, 0)
        End If
    Next cellG2
Next cellG1

End Sub

这两种方法都不会突出显示任何内容,请提供一些信息或为我指明方向。

谢谢。

0 个答案:

没有答案