Delete entire based on another cell value

Sumanthbhe

New Member
Joined
Jan 2, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I need help with Excel VBA code.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 3 And Target.Cells.Count = 1 Then
    If LCase(Target.Value) = "-1" Then
        With Target.EntireRow.ClearContents
        End With
    End If
End If
If Target.Column = 3 And Target.Cells.Count = 1 Then
    If LCase(Target.Value) = "1000" Then
        With Target.EntireRow
            .Copy Sheets("Week Schedule").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
            .Delete
        End With
    End If
End If
End Sub

If the third column we enter -1 it will clear the row. If we enter 1000 it will be copied to another sheet and deleted from the current sheet.

The above code is working fine. Instead of clearing row data, I want to delete that row. So added Line 4 With Target.EntireRow.ClearContents to With Target.EntireRow.Delete But it shows an error.

Someone, please help.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Can you show us your changed code ?
Do you have protection on the sheet ?

The code you have looks a little odd but works in principle, and works fine for me.

It should normally look like either of these 2:-
VBA Code:
       Target.EntireRow.Delete  ' Without using the with

       ' Or using the with
        With Target.EntireRow
               .Delete
        End With
 
Upvote 0
Can you show us your changed code ?
Do you have protection on the sheet ?

The code you have looks a little odd but works in principle, and works fine for me.

It should normally look like either of these 2:-
VBA Code:
       Target.EntireRow.Delete  ' Without using the with

       ' Or using the with
        With Target.EntireRow
               .Delete
        End With
Thanks for your reply.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 3 And Target.Cells.Count = 1 Then
        If LCase(Target.Value) = "-1" Then
            With Target.EntireRow
                .Delete
            End With
        End If
    End If
    If Target.Column = 3 And Target.Cells.Count = 1 Then
        If LCase(Target.Value) = "1000" Then
            With Target.EntireRow
                .Copy Sheets("Week Schedule").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                .Delete
            End With
        End If
    End If
End Sub

Changed the code as per your comment. If I change it to Target.EntireRow.Delete it's not working only EntireRow.ClearContents is working to clear the row.

There is no protection to the working sheet. for other sheets, it's protected to make any changes.
I am not much aware of the VBA code. so I managed to mix two different codes. That's the reason it's looking odd maybe.

It shows the error.
PFA image to check the errors.
 

Attachments

  • vba_code-02.jpg
    vba_code-02.jpg
    84.9 KB · Views: 7
  • vba_code-01.jpg
    vba_code-01.jpg
    16.6 KB · Views: 7
Upvote 0
I can't replicate your issue.
Is the code in the code window of the Sheet, you want it to act on ?
That sheet is NOT protected - right ?
You can try giving us an XL2BB of the sheet it is working on.
I suspect we might need the actual workbook through a shared link though.

I don't think it will make a difference but add the false version to the beginning of the code and the true at the end.
VBA Code:
Application.EnableEvents = False
' ..... the rest of the code .....
Application.EnableEvents = True
 
Upvote 0
You could also try adding the debug.print lines I have here and see what comes up in the immediate window.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    Debug.Print Me.Name
    Debug.Print Target.Address
 
Upvote 0
I have now been able to replicate the issue, try this:-
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False

    If Target.Column = 3 And Target.Cells.Count = 1 Then
        If LCase(Target.Value) = "-1" Then
            With Target.EntireRow
                .Delete
            End With
        ElseIf LCase(Target.Value) = "1000" Then
            With Target.EntireRow
                .Copy Sheets("Week Schedule").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                .Delete
            End With
        End If
    End If

    Application.EnableEvents = True
End Sub
 
Upvote 0
Solution
I have now been able to replicate the issue, try this:-
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False

    If Target.Column = 3 And Target.Cells.Count = 1 Then
        If LCase(Target.Value) = "-1" Then
            With Target.EntireRow
                .Delete
            End With
        ElseIf LCase(Target.Value) = "1000" Then
            With Target.EntireRow
                .Copy Sheets("Week Schedule").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                .Delete
            End With
        End If
    End If

    Application.EnableEvents = True
End Sub
Excellent. It's Working, Thank you so much.
 
Upvote 0
Thanks for letting me know. It was deleting the row when you entered -1, so the “Target” no longer existed when it tried to do the 2nd lot of If Statements.
Glad I could help.
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,600
Members
449,038
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top