2011-11-17 137 views
0

我在excel 2007中使用数据验证。我使用此代码来生成标记为红色圆圈的无效数据。excel vba 2007中特定单元格的CircleInvalid和ClearCircle方法

Private Sub Worksheet_Change(ByVal Target As Range) 

Dim rc As Integer 

Range(Target.Address).Select 

ActiveSheet.ClearCircles 
ActiveSheet.CircleInvalid 

If Not Range(Target.Address).Validation.Value Then 
    rc = MsgBox("Data Validation errors exist! " & Range 
    (Target.Address).Validation.ErrorMessage & " Please correct circled entries!", vbCritical, "Failure") 
    Exit Sub 
End If 
End Sub 

正如你可以在代码中看到,当我把错误的数据,那么:第一,特定范围的是要选择,然后所有的无效数据标有红色圆圈。
但我希望只有特定的单元格应该标记为红色而不是全部数据。

谢谢。

回答

1

您可以从Excel MVP试试这个代码:

Dim TheCircledCell As Range 

Sub CircleCells(CellToCircle As Range) 
    If Not CellToCircle Is Nothing Then 
     With CellToCircle 
      If .Count > 1 Then Exit Sub 
      Set TheCircledCell = CellToCircle 
      .Validation.Delete 
      .Validation.Add xlValidateTextLength, xlValidAlertInformation, xlEqual, 2147483647# 
      .Validation.IgnoreBlank = False 
      .Parent.CircleInvalid 
     End With 
    End If 
End Sub 

Sub ClearCircles() 
If Not TheCircledCell Is Nothing Then 
    With TheCircledCell 
     .Validation.Delete 
     .Parent.ClearCircles 
    End With 
End If 
End Sub 

请注意,您不能使用这些细胞Excel的标准验证功能。

[Source and explanation of the code]