在VBA中进行条件格式化

我打算通过重复的单元格变成红色来pipe理Excel工作表上的重复项目。 我把这个用于表单保护,以防止编辑这些列的条件格式。 但是,当我移动单元格信息(通过单击并拖动)条件格式也从该单元格移动。 在一天结束的时候,我没有重复覆盖每个细胞,我想要的。 有什么方法可以防止这种情况在移动单元时发生,或者我可以用什么macros来处理这个问题?

我想用VBA做这样的事情:

Sub Duplicate() Dim rngData As Range Dim cell As Range Set rngData = Range("P3:P19, P56:P58, P39:P42, P21:P25, P27:P37, P39:P42, P39:P42, P44:P54, M25:M76, B69:B77, B66:E67, B51:B64, H44:H47, D44:D47, H42, H33:H40, D33:D42, H31, D28:D31, H28:H29, D5:D8" & Cells(Rows.Count, "B").End(xlUp).Row) For Each cell In rngData cell.Offset(0, 0).Font.Color = vbBlack ' DEFAULT COLOR ' LOCATE DUPLICATE VALUE(S) IN THE SPECIFIED RANGE OF DATA. If Application.Evaluate("COUNTIF(" & rngData.Address & "," & cell.Address & ")") > 1 Then cell.Offset(0, 0).Font.Color = vbRed ' CHANGE FONT COLOR TO RED. End If Next cell Set rngData = Nothing Application.ScreenUpdating = True End Sub 

但是我得到一个“types不匹配”的错误:如果Application.Evaluate(“COUNTIF(”&rngData.Address&“,”&cell.Address&“)”)> 1然后

我怎样才能解决这个问题?

根据评论你需要循环两次:

 Sub Duplicate() Dim rngData As Range Dim cell As Range Dim cell2 As Range Set rngData = Range("P3:P19, P56:P58, P39:P42, P21:P25, P27:P37, P39:P42, P39:P42, P44:P54, M25:M76, B69:B77, B66:E67, B51:B64, H44:H47, D44:D47, H42, H33:H40, D33:D42, H31, D28:D31, H28:H29, D5:D8" & Cells(Rows.Count, "B").End(xlUp).Row) rngData.Font.Color = vbBlack For Each cell In rngData If cell.Font.Color = vbBlack Then For Each cell2 In rngData If cell = cell2 And cell.Address <> cell2.Address Then cell.Font.Color = vbRed cell2.Font.Color = vbRed End If Next End If Next Set rngData = Nothing Application.ScreenUpdating = True End Sub