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

user2595390

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.

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.

However, when select multiple cells in column B and press delete button, the IsEmpty(Target) returns False. Now here Target is range of multiple cells. I just can't find a way to find if a user has deleted range of values in column B at the same time. 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

Ralph

You misunderstand the purpose of the function 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Excel VBA combining Worksheet_Change codes for 2 target addresses

From Dev

Exclude Cell Range from Worksheet_Change function

From Dev

How to unable Worksheet_Change from Excel VBA if Macro is enabled

From Dev

Worksheet_Change setting target range is slow

From Dev

Excel VBA Worksheet change, entering a range instead of one cell

From Dev

How to exit more than 1 for loop in Excel VBA?

From Dev

Difficulty merging 2 x Private Sub Worksheet_Change (ByVal Target As Range) in one Excel sheet

From Dev

Excel VBA function to lookup value in a Cell range

From Dev

Excel VBA function to lookup value in a Cell range

From Dev

Combining 2 "Private Sub Worksheet_Change(ByVal Target As Range)" into 1

From Dev

Excel VBA target cell on change then ignore

From Dev

how to get more than one cell address out of `find` method in excel vba

From Dev

How to automatically input an array formula as string with more than 255 characters in length into an excel cell using VBA?

From Dev

Worksheet_Change(ByVal Target As Range), Target always equals Nothing

From Dev

How intelligent is excel when is "parses" a function in a cell that includes identical calls to the same function more than once

From Dev

call function to each cell in a range (involve insert rows, so the range will change) in Excel VBA

From Dev

Excel VBA: "Worksheet_Change" function uses ".EntireRow.Insert" and cannot handle it properly

From Dev

Combining Select Case as Cell and Range in Worksheet_Change

From Dev

Excel 2010: How to Run FUNCTION procedures in a cell on another range of cell change?

From Dev

VBA Excel Range() with Cell argument

From Dev

How to count cells in a range with a value less than another cell in excel?

From Dev

Excel VBA Array from Range Starting at -1 rather than 0

From Dev

Excel VBA Array from Range Starting at -1 rather than 0

From Dev

Excel - Restore cell data deleted by VBA?

From Dev

In VBA, I want to make an If statement for when more than one cell in a range contains a value

From Dev

In VBA, I want to make an If statement for when more than one cell in a range contains a value

From Dev

Excel VBA: Worksheet_Change method not working inside my Module

From Dev

Is it possible to delay the Worksheet_Change code? Excel Vba

Related Related

  1. 1

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

  2. 2

    Excel VBA combining Worksheet_Change codes for 2 target addresses

  3. 3

    Exclude Cell Range from Worksheet_Change function

  4. 4

    How to unable Worksheet_Change from Excel VBA if Macro is enabled

  5. 5

    Worksheet_Change setting target range is slow

  6. 6

    Excel VBA Worksheet change, entering a range instead of one cell

  7. 7

    How to exit more than 1 for loop in Excel VBA?

  8. 8

    Difficulty merging 2 x Private Sub Worksheet_Change (ByVal Target As Range) in one Excel sheet

  9. 9

    Excel VBA function to lookup value in a Cell range

  10. 10

    Excel VBA function to lookup value in a Cell range

  11. 11

    Combining 2 "Private Sub Worksheet_Change(ByVal Target As Range)" into 1

  12. 12

    Excel VBA target cell on change then ignore

  13. 13

    how to get more than one cell address out of `find` method in excel vba

  14. 14

    How to automatically input an array formula as string with more than 255 characters in length into an excel cell using VBA?

  15. 15

    Worksheet_Change(ByVal Target As Range), Target always equals Nothing

  16. 16

    How intelligent is excel when is "parses" a function in a cell that includes identical calls to the same function more than once

  17. 17

    call function to each cell in a range (involve insert rows, so the range will change) in Excel VBA

  18. 18

    Excel VBA: "Worksheet_Change" function uses ".EntireRow.Insert" and cannot handle it properly

  19. 19

    Combining Select Case as Cell and Range in Worksheet_Change

  20. 20

    Excel 2010: How to Run FUNCTION procedures in a cell on another range of cell change?

  21. 21

    VBA Excel Range() with Cell argument

  22. 22

    How to count cells in a range with a value less than another cell in excel?

  23. 23

    Excel VBA Array from Range Starting at -1 rather than 0

  24. 24

    Excel VBA Array from Range Starting at -1 rather than 0

  25. 25

    Excel - Restore cell data deleted by VBA?

  26. 26

    In VBA, I want to make an If statement for when more than one cell in a range contains a value

  27. 27

    In VBA, I want to make an If statement for when more than one cell in a range contains a value

  28. 28

    Excel VBA: Worksheet_Change method not working inside my Module

  29. 29

    Is it possible to delay the Worksheet_Change code? Excel Vba

HotTag

Archive