简体   繁体   English

Excel VBA 2007中特定单元的CircleInvalid和ClearCircle方法

[英]CircleInvalid and ClearCircle methods for a particular cell in excel vba 2007

I am using data validation in excel 2007. I am using this code to make invalid data marked with red circle. 我正在Excel 2007中使用数据验证。我正在使用此代码制作带有红色圆圈的无效数据。

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

As you can see in the code when I put wrong data then first of that specific range is going to selected and then all invalid data is marked with red circle. 正如您在代码中看到的那样,当我输入错误的数据时,将首先选择该特定范围,然后所有无效数据都用红色圆圈标记。
But I want that only that specific cell should be marked with red not all data . 但我希望仅将特定单元格标记为红色,而不是所有数据。

Thanks. 谢谢。

You can try this code from an Excel MVP: 您可以从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

Note that you can't use the Excel standard Validation function on these cells. 请注意,您不能在这些单元格上使用Excel标准验证功能。

[ Source and explanation of the code ] [ 代码的来源和说明 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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