Prevent Cell Deletion - Data Validation

meridius10

New Member
Joined
Apr 30, 2011
Messages
47
I have set up a Data Validation List and an Error Alert of 'Stop'.

The spreadsheet has been set up with Protection, locked cells and columns, where necessary.

What I'm looking for is to prevent users deleting any of these particular cells although, of course, they can choose from what is in the data validation list.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Put this code in the worksheet module :

Code:
Option Explicit

Private Declare Sub keybd_event Lib "user32.dll" _
(ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const KEYEVENTF_KEYUP = &H2

Private Const VAL_RANGE As String = "A1" 'change this cell as required.
                                         
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = Range(VAL_RANGE).Address Then
        If IsEmpty(Target) Then
            keybd_event vbKeySpace, 0, 0, 0
            keybd_event vbKeySpace, 0, KEYEVENTF_KEYUP, 0
            keybd_event vbKeyReturn, 0, 0, 0
            keybd_event vbKeyReturn, 0, KEYEVENTF_KEYUP, 0
            With Application
                .EnableEvents = False
                .Undo
                .EnableEvents = True
            End With
        End If
    End If

End Sub

The above assumes the cell with validation is A1 . Change the cell address Constante at the top of the code as needed.
 
Upvote 0
Here's a potential solution. Give it a test.

Start with a blank sheet
Highlight G1:G10
Press the = sign and enter 1
Confirm with Ctrl + Shift + Enter

If entered correctly you will see {=1} in G1:G10

Now try to delete a row.

In cell C3 enter some data validation.

Now try to delete that cell.
 
Upvote 0
OK, but what if I have numerous ranges in multiple columns?


Try this code in the worksheet module which works for a Range with multi-cells. In this case Range A1:D20. Change the range as required

Code:
Option Explicit

Private Declare Sub keybd_event Lib "user32.dll" _
(ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const KEYEVENTF_KEYUP = &H2

Private Const VAL_RANGE As String = "A1:D20" 'change this range as required.
                                         
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range(VAL_RANGE)) Is Nothing Then
          If WorksheetFunction.Sum(Target) = 0 Then
            keybd_event vbKeySpace, 0, 0, 0
            keybd_event vbKeySpace, 0, KEYEVENTF_KEYUP, 0
            keybd_event vbKeyReturn, 0, 0, 0
            keybd_event vbKeyReturn, 0, KEYEVENTF_KEYUP, 0
            With Application
                .EnableEvents = False
                .Undo
                .EnableEvents = True
            End With
        End If
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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