Macro that deletes rows only

Limone

Board Regular
Joined
Dec 20, 2018
Messages
57
Hi.

I have a code that allows the end user to delete selected rows from a protected table. The problem is, as it is now my macro can delete pretty much anything that gets selected and I'm worried that something else outside the table will eventually get deleted by accident, even though I've got an alert message you have to agree to first and everything.

Could someone help me complete my code so that only rows types of objects can get deleted, possibly only if within a specified named range?

Thanks in advance.

Code:
Sub Delete_Click()
'
' Delete_Click Macro
'


'
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Dim answer As Integer
[INDENT]answer = MsgBox("Are you sure you want the selected object to be deleted?", vbYesNo + vbQuestion, "Delete rows")[/INDENT]
[INDENT]If answer = vbYes Then[/INDENT]
[INDENT=2]Selection.Delete Shift:=xlUp[/INDENT]
[INDENT=2]Else if[/INDENT]
[INDENT=3]'do nothing[/INDENT]
[INDENT]End If[/INDENT]
ActiveSheet.Protect
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Does this code do what you want...
Code:
[table="width: 500"]
[tr]
	[td]Sub Delete_Click()
  '
  ' Delete_Click Macro
  '
  Dim Answer As Long
  ActiveSheet.Unprotect
  Answer = MsgBox("Are you sure you want the selected object to be deleted?", vbYesNo + vbQuestion, "Delete rows")
  If Answer = vbYes And TypeName(Selection) = "Range" Then
    If Selection.Columns.Count = Columns.Count Then
      Selection.Delete Shift:=xlUp
    Else
      MsgBox "Wait a minute! You can only delete entire rows and you have selected something other than an entire row or rows! Your action has been cancelled!", vbCritical
    End If
  End If
  ActiveSheet.Protect
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Hi Rick, I've tried your code out. Unfortunately, even though it does ask me for confirmation and no error message comes up, nothing happens after I say yes (that is, even when I try to delete something that's not a row).
I've also changed the "columns" bit to "row" in case that was the problem but it seems to make no difference.
 
Upvote 0
Hi Rick, I've tried your code out. Unfortunately, even though it does ask me for confirmation and no error message comes up, nothing happens after I say yes (that is, even when I try to delete something that's not a row).
I've also changed the "columns" bit to "row" in case that was the problem but it seems to make no difference.
I may have misunderstood what you were looking for... I thought you wanted to allow only entire rows to be deleted. Can you clarify exactly what functionality you want to allow?
 
Upvote 0
Ok, I got what the problem was. It's not possible to stay within the named range while doing what I want to do and that's why it wasn't deleting things.
Thanks a lot for your help!
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,658
Latest member
GStorm

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