简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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