user7415328
user7415328

Reputation: 1083

VBA: Delete target row if cell contains text?

I have a spreadsheet like so:

Column Y Remove Remove Remove Remove

I want the user to be able to delete each row by clicking each of the remove texts. This should delete only the row which the user has clicked remove for, aka the target row.

Here is my code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 If Target.Column = 25 And Target.Value = "Remove" Then
Range("A" & ActiveCell.Row).EntireRow.Delete
 End If
End Sub

for some reason this does nothing.

I also want to add a condition which only deletes those rows from row 17 onwards.

Please can someone show me how to get this working correctly? thanks

Upvotes: 1

Views: 1594

Answers (2)

JoeFox
JoeFox

Reputation: 1081

For me it worked with the Workbook_SheetSelectionChange event.

Try this:

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
 If Target.Column = 25 And Target.Value = "Remove" Then
    Range("A" & ActiveCell.Row).EntireRow.Delete
 End If
End Sub

Also you can verify that events are enabled by running:

Sub EnableEvents()
  Application.EnableEvents = True
End Sub

Upvotes: 1

user3598756
user3598756

Reputation: 29421

try this

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Target
        If .Column = 25 And .Row > 16 And .value = "Remove" Then .EntireRow.Delete
    End With
End Sub

Upvotes: 1

Related Questions