Preventing/Limiting User from Deleting Data Out of Specified Range

ybr_15

Board Regular
Joined
May 24, 2016
Messages
204
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi dear,

I have this code to delete data based on selection range:
Code:
Sub DeleteSelection()
    Dim myrange As Range

    On Error Resume Next
    Set myrange = Application.InputBox(Prompt:="Select your range:", Title:="Delete Data", Type:=8)
    If Not myrange Is Nothing Then
        myrange.ClearContents
    End If
End Sub
Now, I want to add a condition to delete data so that users can only delete data in the specified area, eg. Range (A5:C100) and Range (F5:H50). Users are not permitted to delete data outside that range (event though it is intersect). Can anyone here help me? Thanks
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try replacing
Code:
myRange.ClearContents

with
Code:
Intersect( myRange, Range("A5:C100") ).ClearContents
Intersect( myRange, Range("F5:H50") ).ClearContents

or do you only want user to be able to delete from ONE of the ranges ?
If so, how does VBA know which range to choose ?
Should user make decision based on reply to a message ?
or delete from first range only ?
 
Last edited:
Upvote 0
Hi Yongle,

What I want is users free to delete data based on his/her SELECTION in the Range (A5:C100) and Range (F5:H50). But, if their SELECTION is OUT OF RANGE or INTERSECT out of specified range (Range (A5:C100) and/or Range (F5:H50)) then the data will not be deleted. User only ALLOWED to delete data if their SELECTION is WITHIN the specified range.

Example:
If user want to delete range(A5:D5) then data will not be deleted because selection is intersect D5 (which is out of the specified range). But, if they want to delete range(A5:C5) then the data will be deleted because selection is within the specified range. Can it be done with vba? Thanks
 
Upvote 0
One way

Code:
Sub DeleteSelection()
    Dim myRange As Range, C As Long, C10 As Long, C20 As Long
    On Error Resume Next
    Set myRange = Application.InputBox(Prompt:="Select your range:", Title:="Delete Data", Type:=8)
    C = myRange.Count
    C10 = Intersect(Range("A5:C300"), myRange).Count
    C20 = Intersect(Range("F5:H50"), myRange).Count
    If C10 > 0 And C = C10 Then
        myRange.ClearContents
    ElseIf C20 > 0 And C = C20 Then
        myRange.ClearContents
    End If
End Sub
 
Upvote 0
Hi Yongle,

Thanks for your help but your code doesn't work the way I want it to. Just now, I tried (reads and trials) and finally got it:
Code:
Sub DeleteSelection()
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim shtReference As Worksheet
    Dim rngMyRange As Range
    Dim rngIntersect As Range

    Set shtReference = wb.Sheets("MyData")
    
    On Error Resume Next
    Set rngMyRange = Application.InputBox(Prompt:="Select your range:", Title:="Delete Data", Type:=8)
    Set rngIntersect = Application.Intersect(rngMyRange, shtReference.Range("Range_Data"))
    If Not rngIntersect Is Nothing Then
        If Not rngMyRange Is Nothing Then
            With rngIntersect [COLOR=#008080]' I think this is the problem because I choose [/COLOR][COLOR=#8b4513]rngMyRange[/COLOR][COLOR=#008080] instead of [/COLOR][COLOR=#8b4513]rngSelect[/COLOR][COLOR=#008080] before[/COLOR]
                .ClearFormats
                .ClearContents
            End With
        End If
    End If
End Sub

Once again, thanks for your help. Sorry if my question is confusing (I still learn a lot about VBA ;))
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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