简体   繁体   中英

How to check if the content of the Target cell is deleted in a Worksheet_Change Event

I want to either delete the validation of a cell or set an empty validation if the Target of Worksheet_Change is deleted.

The Target cell is a merged cell which also has a xlValidateList . If I choose one of the values, my code runs ok, but when I delete the content of this cell, it doesn't change the validation of the other cell.

I think it comes from the merging of the cells or because it has a xlValidateList

I tried to check IsEmpty(Target) but its always FALSE, even if I delete the content of the Target cell

My code so far:

Private Sub Worksheet_Change(ByVal Target As range)

Dim lengthFromCell As range
Dim lengthToCell As range

' only execute when on column F and
If Target.Column <> 6 Or Target.Cells.Count > 1 Then Exit Sub

Set lengthFromCell = Target.Offset(0, 1)
Set lengthToCell = lengthFromCell.Offset(1, 0)

' Delete contents of "length" cells
lengthFromCell.value = ""
lengthToCell.value = ""
    
    If Target.value = "A" Then
        With Target.Offset(0, 1).Validation
            .Delete
            .Add Type:=xlValidateList, _
                AlertStyle:=xlValidAlertStop, _
                Operator:=xlBetween, _
                Formula1:="1, 2, 3"
            .IgnoreBlank = True
            .InCellDropdown = True
            .ErrorTitle = ""
            .ErrorMessage = ""
            .ShowError = True
        End With
    ElseIf Target.value = "B" Then
        With Target.Offset(0, 1).Validation
            .Delete
            .Add Type:=xlValidateList, _
                AlertStyle:=xlValidAlertStop, _
                Operator:=xlBetween, _
                Formula1:="4, 5, 6"
            .IgnoreBlank = True
            .InCellDropdown = True
            .ErrorTitle = ""
            .ErrorMessage = ""
            .ShowError = True
        End With
    Else
'here either delete the validation or at least set it to 0 or ""
        With Target.Offset(0, 1).Validation
            .Delete
            .Add Type:=xlValidateList, _
                AlertStyle:=xlValidAlertStop, _
                Operator:=xlBetween, _
                Formula1:="0, 0, 0"
            .IgnoreBlank = True
            .InCellDropdown = True
            .ErrorTitle = ""
            .ErrorMessage = ""
            .ShowError = True
        End With
    End If
End Sub

EDIT:

"Target" is the target from Worksheet_Change which is of type Range . So it is the merged cell in which my dropdown list with values "A and B" are in.

细胞图片

The main problem is that my code doesn't recognice the deletion of the value "A" in merged cells, but in single cells it does.

Please, try the next function:

Function isMergeEmpty(rng As Range) As Boolean
   Dim x As String
   On Error Resume Next
      x = rng.value
      If err.Number <> 0 Then
            isMergeEmpty = True
      End If
    On Error GoTo 0
End Function

It can be called from the event in this way:

Private Sub Worksheet_Change(ByVal Target As Range)
    If isMergeEmpty(Target) Then 
        'do here what you need...
        MsgBox "Empty merge cell..."
    End if
End Sub

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