Removing Duplicates is affecting another macro

时间:2015-12-31 19:40:58

标签: excel-vba excel-2007 vba excel

We run multiple macros on a worksheet. One of them is used to remove duplicates.

Sub Remove_Duplicates()
'
'
Application.ScreenUpdating = False

Call Convert_Text_To_Number

    Cells.Select
    ActiveSheet.Range("$A$1:$AE$2000").RemoveDuplicates Columns:=Array(10, 11, 12, 13, 14, 15, 16), Header:=xlYes
    ActiveWindow.SmallScroll Down:=6

Range("C" & Rows.Count).End(xlUp).Offset(1).Select

Application.ScreenUpdating = True

End Sub

We copy a set of data from one worksheet into our main workbook. This "new" data occupies A2:Z* (I used an asterisk because there is no set limit to the number of rows and we are adding data daily). When we run our comparisons to another data set, we input (manually) new information into columns AA:AE for each completed row. There is a code in place that changes the text color for the entire row to green once a date is inserted into column AE for that respective row:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim r As Range
Set r = Target.EntireRow

    If Target.row = 1 Then Exit Sub 'don't change header color

    If r.Cells(1, "AD").Value <> "" Then
        r.Font.Color = RGB(0, 176, 80)
    Else
        r.Font.Color = RGB(0, 0, 0)
    End If

End Sub

(The above code is inserted in the Sheet1 object of the workbook) After this code was added, each time we run the Remove Duplicates macro, all unique lines that remain are now changed to green text, but there is no date in column AE of that row. I did find that if I go to that cell, hit delete and go to another cell, that row will revert back to black text. Can anyone help me determine what is causing this to occur? It seems like there is a phantom value inserted into the cells.

1 个答案:

答案 0 :(得分:1)

您认为目标是某个单独的单元格,但事实并非如此。 可能是单个单元格,但在删除重复命令中,它更可能是单元格行。您不能使用Target.EntireRow处理像单个单元格这样的大组单元格。

示例:Range("A2").EntireRowRange("A2:AD50").EntireRow

不同

以下内容应指向正确的方向。

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rw As Long, rwRng As Range

    For Each rwRng In Target.Rows
        If rwRng.Row > 1 Then
            If Cells(rwRng.Row, 30).Value <> "" Then  'column AD is column 30
                rwRng.Font.Color = RGB(0, 176, 80)
            Else
                rwRng.Font.ColorIndex = xlAutomatic
            End If
        End If
    Next rwRng

End Sub