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: 0

Some videos you may like

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

PHSYT

New Member
Joined
Mar 1, 2006
Messages
5
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
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,238
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,412
Office Version
  1. 365
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,127,318
Messages
5,623,974
Members
416,002
Latest member
Neshx

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
Top