Excel-VBA - How to identify Target range more than 1 cell is deleted in a Worksheet Change function

0 votes

I'm attempting to determine whether the worksheet change function will cause cells in the same rows in column X to also be deleted if a user deletes values from certain cells in column B.

IsEmpty(Target) returns true when I delete just one cell in column B, allowing me to erase the identical row cell in column X.

IsEmpty(Target) returns False when many cells in column B are selected and the delete button is pressed. Target is currently a range of several cells. Simply put, I am unable to determine whether a user simultaneously erased a range of numbers in column B. Any assistance would be greatly valued.

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

Can someone please help me with this?

Sep 22, 2022 in Others by Kithuzzz
• 38,010 points
3,271 views

1 answer to this question.

0 votes

You misunderstand the purpose of the function IsEmpty

I assume the cells without a value are what you're truly looking for (blank cells). The number of cells with values is displayed in the next line. If it equals 0, they are all left empty.

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
answered Sep 23, 2022 by narikkadan
• 63,700 points

Related Questions In Others

0 votes
1 answer

How to check if a cell is empty in a range variable?

Use WorksheetFunction.CountA() (https://learn.microsoft.com/en-us/office/vba/api/excel.worksheetfunction.counta) function If WorksheetFunction.CountA(rng) = 0 Then ...READ MORE

answered Jan 15, 2023 in Others by narikkadan
• 63,700 points
628 views
0 votes
1 answer

How do I use the Indirect Function in Excel VBA to incorporate the equations in a VBA Macro Function

Try this: Sub Test() Dim str As String: str ...READ MORE

answered Jan 19, 2023 in Others by narikkadan
• 63,700 points
759 views
0 votes
1 answer

How to automatically get a specified range of data from Excel to XML in VBA

Range method works, always identify the sheet ...READ MORE

answered Mar 18, 2023 in Others by narikkadan
• 63,700 points
620 views
0 votes
1 answer

How to programmatically get the values of a spilled Excel range in VBA?

By using the Text property, I was ...READ MORE

answered Mar 23, 2023 in Others by narikkadan
• 63,700 points
811 views
0 votes
1 answer

How do I get it to select a single row based on the value?

Try this: Sub MoveBasedOnValue2() Dim ...READ MORE

answered Jan 20, 2023 in Others by narikkadan
• 63,700 points
443 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
974 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,316 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
627 views
0 votes
1 answer

Excel VBA : HOW TO PRINT THE TEXT IN A CELL (like Wrap Text)

Use a LineFeed character to create a ...READ MORE

answered Oct 27, 2022 in Others by narikkadan
• 63,700 points
851 views
0 votes
1 answer

Is there a way to test a formula result in excel and type it only once, all within one cell and not using a user defined function?

Use the Let function: =LET(Value,A1+B2+C4+G3+B4,IF(Value>10,"No",Value)) I hope this helps ...READ MORE

answered Jan 9, 2023 in Others by narikkadan
• 63,700 points
469 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP