简体   繁体   English

Excel-VBA - 如何识别在Worksheet_Change函数中删除目标范围(超过1个单元格)?

[英]Excel-VBA - How to identify Target range (more than 1 cell) is deleted in a Worksheet_Change function?

I am trying to find if a user deletes values in certain cells in column B then cells in same rows in column X are also deleted using worksheet_change function. 我试图找到用户是否删除列B中某些单元格中的值,然后使用worksheet_change函数删除列X中相同行中的单元格。

When I delete only one cell in column B then IsEmpty(Target) returns true and I am able to clear the same row cell in column X. 当我只删除B列中的一个单元格时,IsEmpty(Target)返回true,我能够清除X列中的同一行单元格。

However, when select multiple cells in column B and press delete button, the IsEmpty(Target) returns False. 但是,当在列B中选择多个单元格并按删除按钮时,IsEmpty(目标)将返回False。 Now here Target is range of multiple cells. 现在这里Target是多个细胞的范围。 I just can't find a way to find if a user has deleted range of values in column B at the same time. 我找不到一种方法来查找用户是否同时删除了B列中的值范围。 Any help would be much appreciated. 任何帮助将非常感激。

Below code works when one cell is deleted but not when a range of cells are deleted. 下面的代码在删除一个单元格时起作用,但在删除一系列单元格时起作用。

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Column <> 2 Then Exit Sub
    If Target.Columns.Count > 1 Then Exit Sub

    If IsEmpty(Target) Then

        Application.EnableEvents = False
        ActiveSheet.Range("X" & Target.Row & ":X" & Target.Row + Target.Rows.Count - 1).ClearContents
        Application.EnableEvents = True
    End If

End Sub

Thanks Uttam 谢谢Uttam

Copy the column in question into a hidden worksheet ( workSheet.Visible = xlVeryHidden ), then use WorksheetFunction.CountA to compare the number of non-empty cells in the column of the visible worksheet and in the 'shadow' column of the hidden worksheet. 将相关列复制到隐藏工作表( workSheet.Visible = xlVeryHidden )中,然后使用WorksheetFunction.CountA比较可见工作表列和隐藏工作表“阴影”列中的非空单元格数。 This will quickly inform you if the number of cells with contents has decreased (ie, contents have been deleted). 如果包含内容的单元格数量减少(即内容已被删除),这将快速通知您。

If so, get the .UsedRange of the observed column, find the first empty cell in it and check if the corresponding cell in the shadow column is empty as well. 如果是这样,获取观察列的.UsedRange ,找到其中的第一个空单元格,并检查影子列中的相应单元格是否也为空。 If yes, continue to the next empty cell; 如果是,继续下一个空单元格; if not, you know the cell content has been deleted, and you can delete the corresponding cell content in your 'column X'. 如果没有,您知道单元格内容已被删除,您可以删除“X列”中相应的单元格内容。

After each Worksheet_Change event you need to make another 'shadow' copy. 在每个Worksheet_Change事件之后,您需要制作另一个“影子”副本。

You misunderstand the purpose of the function IsEmpty . 你误解了IsEmpty函数的用途。

I guess what you are really looking for are cells which do not contain a value (blank cells). 我猜你真正想要的是不包含值的单元格(空白单元格)。 The following line will give you the count of cells which contain a value. 以下行将为您提供包含值的单元格计数。 If that is equal to zero then they are all blank. 如果它等于零,那么它们都是空白的。

Target.SpecialCells(xlCellTypeConstants).Count

Yet, the above line of code will result in an error if all cells are empty. 但是,如果所有单元格都为空,则上面的代码行将导致错误。 Hence, you will have to adjust your code as follows: 因此,您必须按如下方式调整代码:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column <> 2 Then Exit Sub
If Target.Columns.Count > 1 Then Exit Sub

Dim bolIsEmpty As Boolean

On Error GoTo AllAreEmpty
bolIsEmpty = Target.SpecialCells(xlCellTypeConstants).Count = 0
On Error GoTo 0

If bolIsEmpty Then
    ' ... your code here ...
End If

Exit Sub

AllAreEmpty:
    bolIsEmpty = True
    Resume Next

End Sub

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

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