how do i protect a cell from being change but allow others

beeman

New Member
Joined
Jun 1, 2011
Messages
17
I have been tasked with writing a user friendly analytical template at work. It is for some of our staff that has very poor excel skills. I know more and better training would be the more logical approach, however they are all 5 years and lower to retirement and this would be harder then you can possibly imagin.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
What I have is a template with two buttons. One to add a line and copy the previous row formulas, as follows:
<o:p></o:p>
Private Sub CommandButton4_Click()<o:p></o:p>
'make new row<o:p></o:p>
ActiveCell.EntireRow.Insert shift:=xlDown<o:p></o:p>
'copy the row<o:p></o:p>
ActiveCell.Offset(1).EntireRow.Copy Cells(ActiveCell.Row, 1)<o:p></o:p>
On Error Resume Next<o:p></o:p>
End Sub
<o:p></o:p>
And another to delete a line
<o:p></o:p>
Private Sub CommandButton3_Click()<o:p></o:p>
'to delet row<o:p></o:p>
ActiveCell.EntireRow.Delete<o:p></o:p>
End Sub
<o:p></o:p>
They both work great provided the excel worksheet isn’t protected.
<o:p></o:p>
My problem is on the rows I want to be able to add a line and delete a line I also have some formulas I need protected. When I protect the document these macro’s stop working.
<o:p></o:p>
If I set the macro up to unlock the page do the change and then relock it non of the cells are protected from the delete button. Some of the staff are click happy and would end up deleteing the entire document before they relize it.
<o:p></o:p>
What I need is either a code to prevent these macro’s from working on a specified area but allow it on another. (This area would of course always change as rows are added and deleted), or I need a different way to protect my regular formulas and headings so these macro’s will work.
<o:p></o:p>
Thanks for your help and time.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
The worksheet protect method has a "UserInterfaceOnly" property that you can use when you protect the sheet via code. This allows macros to change the sheet but won't allow changes from the GUI.

Sounds like that property may be of use to you.

Gary
 
Upvote 0
Thanks for the reply
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
Forgive my ignorance but I don’t know what a GUI is.
<o:p> </o:p>
The “UserInterfaceOnly” look like it will work great for my add line button, but will it protect my headings from the delete button?
 
Upvote 0
Code:
I don’t know what a GUI is.

Graphical User Interface, the Excel windows, toolbars etc.

will it protect my headings from the delete button

I guess it depends on how you set up the protection. If you use “UserInterfaceOnly”, the only way you'll be able to change/delete protected items is via code and that, hopefully, will be completely under your control.

Gary
 
Upvote 0
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
Thanks <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:City w:st="on"><st1:place w:st="on">Gary</st1:place></st1:City>
<o:p> </o:p>
I get it now you are saying I should set up a tool bar with prompts in it to populate my sheet. I didn’t consider that.
<o:p> </o:p>
I still don’t understand how I could exclude my headings from a delete button using “UserInterfaceOnly”
<o:p> </o:p>
Should I be using a different code for the delete button or would it be possible to restrict the macro from deleting specific words.
 
Upvote 0
You'll have to be more specific in exactly what you want. There are all kinds of ways to stop users from doing certain things. For example, you could disable the delete key and remove all the delete buttons from the GUI. That way the only way anything could be deleted is via your macro where you can decide if delete is allowed.

Gary
 
Upvote 0
I understand it now
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
I keep thinking of doing a stand alone macro instead of a GUI. I will write and set one up and then take it from there.
<o:p> </o:p>
Thanks for all your help Gary

--------------------------------------------------------------------------------
I don’t fail. I succeed at finding out what doesn’t work.
<o:p> </o:p>
Beeman
 
Upvote 0
Here's a snip for the Workbook Selection Change event that won't let you select row 1 (headers). The theory being "if they can't select it they can't delete/change it". If the user wants to change/delete something in row 1 they must do it via your macro.

Gary

Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

Application.EnableEvents = False
If Target.Cells(1).Row = 1 Then
    MsgBox "You may not select row 1"
    Target.Offset(1, 0).Select
End If
Application.EnableEvents = True

End Sub
 
Upvote 0
<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:City w:st="on"><st1:place w:st="on">Gary</st1:place></st1:City><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
Great Idea works perfectly<o:p></o:p>
<o:p> </o:p>
I can use this instead of the password protection on entire columns.<o:p></o:p>
<o:p> </o:p>
Is there anyway to set this code to stay with a particular row if I insert one in fount of it. For example. I set row 14 to prevent selection my end total row, and insert a row above it. I now want row 15 to prevent selection.<o:p></o:p>
<o:p> </o:p>
This will save me so much time by not having to do a GUI
<o:p></o:p>
--------------------------------------------------------------------------------
I don’t fail at things. I succeed at finding out what doesn’t work.
<o:p> </o:p>
Beeman
 
Upvote 0
Here's something similar. This sample uses a named range to track the row of interest. Name the range (entire row) "Test" for this demo.

Note that if there is ever an error in this procedure events will never be re-enabled and it will stop working. If this happens, you can copy/paste "Application.EnableEvents = True" (no quotes) into the immediate (debug) window and press enter to reset it.

You must have the Enable & Disable events in this procedure if you intend to change the selection while you are in the procedure otherwise it causes this procedure to call itself recursively. Under certain conditions this can cause your machine to run out of memory or stack space etc ... major problem.

Gary

Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

Dim oIsect As Range
Application.EnableEvents = False

'The following requires the entire row to be a named range called "Test"
Set oIsect = Application.Intersect(Target, ActiveSheet.Range("Test"))
If Not oIsect Is Nothing Then
    MsgBox "Row " & oIsect.Cells(1).Row & " is not selectable"
    oIsect.Offset(1, 0).Select
End If
Application.EnableEvents = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,605
Messages
6,179,860
Members
452,948
Latest member
UsmanAli786

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