prevent deleting formula by validation

merlin_the_magician

Active Member
Joined
Jul 31, 2002
Messages
480
need some serious Excel-knowledge here people…

I’m very proud of myself, for creating an awesome spreadsheet that is that bloody good it will be spread to everyone throughout the office :biggrin: … one minor thing however…

I have a few formula-cells that display specific info-text when selected, and display specific error-message when data is entered, because I limited input to ZERO characters, so overwriting is not possible.

Entering values is impossible, because of the validation, however it is possible to delete the formula. :mad: :mad:
How can I prevent formulas from getting deleted? I DO NOT want to use common password-protection, because I want to specify the error message myself by using validation.

Anybody know how to do this? :confused: Been searching the web, but no result…
This message was edited by merlin_the_magician on 2002-08-01 04:31
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I guess you could use a little bit of code, like that below, but to be honest, you're much better off locking the cells you don't want to be changed and protecting the sheet- that's what it's there for after all :).

Right-click on the sheet tab, select view code and paste this in. This will prevent anyone from selecting a cell in column A, so you'll probably need to adapt it to your own needs.
<code>
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

If Target.Column = 1 Then
MsgBox "You cannot change this cell."
Cells(Target.row, 2).Select
End If

End Sub
</code>
 
Upvote 0
By this one will prohibit people from selecting a cell. This can be done by security too.

What i want is people to be able to select the cell, because validated text tells people what the cell does.

I just do not want them to be able to enter any charcter (validated the cell to ZERO characters).
Thing is... altough altering the formula is not possible, deleting however is.

When people try to delete the formula after all, i want to give them a specific error message, telling them why they cannot delete the formula. This does not work with common security, because this will give a standard popup.
 
Upvote 0
Just include the text you want to display when people do select the cell in the MsgBox statement. They can still enter the cell, they just can't do anything while they're there and once they click OK on the pop-up they'll be dumped back out to the cell next to it. As an example change to the below and try it out on a blank workbook: -

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

If Target.Column = 1 Then
MsgBox "You cannot change this cell. This cell calculates the sum of cells A1:A100."
Cells(Target.row, 2).Select
End If

End Sub
 
Upvote 0
Another way, I guess, would be to unlock the cells you want people to be able to change, Insert Comment on one of the formula cells, then copy this and paste special- comments to the rest of your formula cells.
 
Upvote 0
Try this one, it'll work on any cell which has a formula in it. You might want to add some logical statements if you have different formulas, etc and make sure it'll never select a cell which already has a formula in it.

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

If Target.Count > 1 Then Exit Sub
If Target.HasFormula = True Then
MsgBox "You cannot change this cell. This cell calculates the sum of cells A1:A100."
Target.Offset(0, 1).Select
End If

End Sub
This message was edited by Mudface on 2002-08-01 07:01
 
Upvote 0
I love it!!!

But... when i want to protect a specific cell the following line does not work:

If Target.Address = $A$1 Then

Excel chockes on the '$' sign...
 
Upvote 0
Sorry, ignore that and my edited post above, that wouldn't work properly if you selected a group of cells including your formula cell. Try the following (hopefully last :) ) piece of code, change myRange to reflect the cell(s) you want: -

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Dim myRange As Range

Set myRange = Range("A1")
Set isect = Application.Intersect(myRange, Target)
If Not isect Is Nothing Then
MsgBox "You cannot change this cell. This cell calculates the sum of cells A1:A100."
Cells(1, 2).Select
End If

End Sub
 
Upvote 0
This is exactly what i meant!

(i might be getting a pain in the neck now... but one more question...)

How to specify several cells that are spread out? (so not just D15 and D16, but also I12, X33 and AB4)
This message was edited by merlin_the_magician on 2002-08-01 07:50
 
Upvote 0

Forum statistics

Threads
1,215,159
Messages
6,123,351
Members
449,097
Latest member
thnirmitha

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