Using a selection from a Data Validation list to delete a row

PHSYT

New Member
Joined
Mar 1, 2006
Messages
5
I am trying to find workarounds for users to insert and delete rows in a protected sheet/table.
I have UserInterFaceOnly:=True and have a simple macro to insert a row at the desired location. This is fine as the row will always insert in the same place, but having a simple macro to delete a row can remove any row (including those that should never be deleted). The best solution i can think of (and one which i like) would be to have a macro that deletes a row if the user selects 'Delete' from a data validation list (of that particular row).

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.Column = 1 Then
        ThisRow = Target.Row
        If Target.Value = "Delete" Then
        '    Range("B" & ThisRow).Interior.ColorIndex = 3
        '    Target.Row("1:1").EntireRow.Select
        Target.Rows.EntireRow.Select
            Selection.Delete Shift:=xlUp
        Else
            Range("B" & ThisRow).Interior.ColorIndex = xlColorIndexNone
        End If
    End If
End Sub


the macro successfully deletes the row but keeps throwing an error back at me (operator type mismatch) and highlights 'If Target.Value = "Delete" Then' part of the macro.
I presume it has an issue with the target value now being deleted?
i didnt want to put in something to ignore the error and end if the macro is fundamentally flawed.

Can anyone help me?
 

Attachments

  • Screenshot 2020-12-14 112824.png
    Screenshot 2020-12-14 112824.png
    23.9 KB · Views: 5

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
sorry... cleaner VBA code

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.Column = 1 Then
        ThisRow = Target.Row
        If Target.Value = "Delete" Then
        Target.Rows.EntireRow.Select
            Selection.Delete Shift:=xlUp
        Else
        End If
    End If
End Sub
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    If Target.Value = "Delete" Then
        Rows(Target.Row).Delete
    End If
    Application.EnableEvents = True
End Sub
 
Upvote 0
Another option
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column = 1 Then
        If Target.Value = "Delete" Then
            Target.EntireRow.Delete
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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