简体   繁体   中英

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.

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:

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.

[ Source and explanation of the code ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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