VBA deletes a row but only works in within a range

APML

Board Regular
Joined
Sep 10, 2021
Messages
216
Office Version
  1. 365
Hi all, I'm using the following VBA in a worksheet (the worksheet is called DataEntry), and would appreciate help with the following :

Sub DeleteRow()
Rows(ActiveCell.Row).Delete
End Sub

I need to amend it, so it can only work with the named range called "Database" which is a section of the worksheet called DataEntry
i.e so if I'm on a cell not within that range called "Database" then I don't want the VBA to operate, but would be great if a message popped up that said "Unable to delete data from this section"
Also would be great when in the range called "Database" (and the VBA triggered) to get a popup message that says "Are you sure you want to delete this Data" (yes or no)
before the VBA operates (if no, then VBA doesn't operate)
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Untested.
VBA Code:
Sub DeleteRow()
Dim Ans
If Not Intersect(ActiveCell, Range("Database")) Is Nothing Then
    Ans = MsgBox("Are you sure you want to delete this Data?", vbYesNo)
    If Ans = vbYes Then
        Rows(ActiveCell.Row).Delete
    End If
Else
    MsgBox "Unable to delete data from this section"
End If
End Sub
 
Upvote 0
Untested.
VBA Code:
Sub DeleteRow()
Dim Ans
If Not Intersect(ActiveCell, Range("Database")) Is Nothing Then
    Ans = MsgBox("Are you sure you want to delete this Data?", vbYesNo)
    If Ans = vbYes Then
        Rows(ActiveCell.Row).Delete
    End If
Else
    MsgBox "Unable to delete data from this section"
End If
End Sub
Hi, thx for your help. I've tested it and found it does delete when outside of the range called "database". ( the message pops up saying "unable to delete from this section"but it still deletes the row)
 
Upvote 0
Hi, thx for your help. I've tested it and found it does delete when outside of the range called "database". ( the message pops up saying "unable to delete from this section"but it still deletes the row)
I can't reproduce that result. If the activecell is not in the range named "Database" the first part of the first If statement will be bypassed, the msgbox "Unable to delete data from this section" will be activated and the routine will be ended when the user closes the msgbox. There's no way I can see any deletion happening if you have used the code I posted exactly as is. Have you modified the code or tried to embed it in some longer code?
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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