Search TeachExcel.com
TeachExcel.com
TE
Teach Excel MS Office Tutorials Excel Consulting Services Excel Forum
Excel Video Tutorials Excel Tips Free Excel Macros Excel Help Resources Contact TeachExcel
Video Tutorials
  • Free Macros
  • Excel Help Directory
  • Excel 2007 Resources
  • Keyboard Shortcuts
  • Excel Forum
  • Contact/About

Macros
Excel Tutorials For Macros

Hide Formulas in a Worksheet and Prevent Deletion


Bookmark and Share

This macro will hide all formulas within a workbook and not allow them to be deleted. The page will not be protected like a regular protected worksheet in excel; this means that you can still add content to the workbook without a problem and you can edit everything as long as it is not a formula. You can enter a new formula but you cannot then delete that formula. In addition, if you select a cell with a formula, that cell's contents will not be displayed. This allows you to keep your formulas hidden from users and adds an extra level of security. If you want to edit a particular cell, you will have to click that cell and then "Unhide" that cell by typing a password (which is located in the VBA code). Also, if you decide to delete this macro from your workbook, you will have to re-enter the password to unprotect the workbook afterwards if you want everything to be unprotected.

Note: To change the password, locate where it says "password" in the macro vba code and change this word to any password you want. You will need to change this in three separate places within the macro.
Where to install the macro:  ThisWorkbook

Hide Formulas in a Worksheet and Prevent Deletion

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

Dim formula As Range

On Error Resume Next

   Sh.Unprotect Password:="password"

   With Selection

   .Locked = False

   .FormulaHidden = False

End With

If Target.Cells.Count = 1 Then

If Target.HasFormula Then

   With Target

   .Locked = True

   .FormulaHidden = True

End With

   Sh.Protect Password:="password", UserInterFaceOnly:=True

End If

   ElseIf Target.Cells.Count > 1 Then

   Set formula = Selection.SpecialCells(xlCellTypeFormulas)

   If Not formula Is Nothing Then

   With Selection.SpecialCells(xlCellTypeFormulas)

   .Locked = True

   .FormulaHidden = True

End With

   Sh.Protect Password:="password", UserInterFaceOnly:=True

End If

End If

   On Error GoTo 0

End Sub


Bookmark and Share


How to Install the Macro
  1. Select and copy the text from within the grey box above.

  2. Open the Microsoft Excel file in which you would like the Macro to function.

  3. Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.  Or For other ways to get there, Click Here.

      For Excel Versions Prior to Excel 2007
      Go to Tools > Macros > Visual Basic Editor

      For Excel 2007
      Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic

  4. On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.

  5. If the Macro goes in a Module, Click Here, otherwise continue to Step 8.

    1. Go to the menu at the top of the window and click Insert > Module
    2. Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
    3. Go to Step 8.

  6. If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.

    1. Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
    2. Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
    3. A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
    4. Go to Step 8.

  7. If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.

    1. Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
    2. Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
    3. A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
    4. Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
    5. Go to Step 8.

  8. Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.

  9. You are now ready to run the macro.



Similar Helpful Excel Resources

Prevent Deletion - Excel

View Content
Hello Everyone, Not sure if this is possible, from what i read its not.

I need to prevent a user from deleting my Excel 2007 file but still allow them to edit the file. I know excel deletes the file and then renames a temporary file back to the original file's name each time you save the document. I have played around with the traverse permission but with no luck. My Idea was to use the traverse permission on the containing folder to allow user to access the file via a shortcut but not directly.

Excel 2007
Windows XP Pro

Any ideas would be grateful.

Prevent Row Deletion ? - Excel

View Content
Hello,

Is there a way to prevent deletion of rows in a worksheet & allow everything else i.e insert, update etc,etc.

i tried this using Tools > Protect > Protect Sheet by only ticking 'Delete Rows' but this stops me from doing EVERTHING ?!

Dhimit

Prevent Deletion Of Certain Cells - Excel

View Content
I have the following scenario:

A user is allowed to edit a worksheet, but certain cells are not to be deleted (they can be changed, but not deleted). Is it possible to have a macro that examins the content of a cell before allowing to deleting it? An Application.BeforeDeleteEvent would be perfect, but I can't find any that matches my request.

Prevent Deletion Of Chart - Excel

View Content
Dear Forum Members,
.
Is that a way in vba to prevent users from deleting charts?

Prevent Sheet Deletion - Excel

View Content
Quote:

Originally Posted by Dave Peterson

And if you have multiple workbooks open and go from the Help sheet to a
different workbook, then the worksheet_deactivate event won't fire.

You may want to add Workbook_Activate and Workbook_Deactivate code, too.

Dave Peterson



Hey dave,
I have implemented the code norman had suggested, and it's working for me
although i'm getting the problem you have described
I'll be in an excel file with out any code at all and not linked to the workbook with the delete prevention code
and i'll go to delete a sheet in the file without any code, and my workbook with the code will open and then the message to prevent the deletion will pop up

Are you saying i'd have to add those activate and deactivate commands to any new workbook file i make? a little confused on how to prevent this from happening
obviously i only want this prevent deleting to be in that one particular file and not be applied to anything else

Code:

Private Sub Worksheet_Activate()
Dim CB As CommandBar
Dim Ctrl As CommandBarControl
For Each CB In Application.CommandBars
Set Ctrl = CB.FindControl(ID:=847, recursive:=True)
If Not Ctrl Is Nothing Then
Ctrl.OnAction = "RefuseToDelete"
Ctrl.State = msoButtonUp
End If
Next
End Sub


Code:

Private Sub Worksheet_Deactivate()
Dim CB As CommandBar
Dim Ctrl As CommandBarControl
For Each CB In Application.CommandBars
Set Ctrl = CB.FindControl(ID:=847, recursive:=True)
If Not Ctrl Is Nothing Then Ctrl.OnAction = ""
Next
End Sub


The above represents worksheet event code and should be pasted into the Help
sheets's code module (not a standard module and not the workbook's
ThisWorkbook module):

************************************************************
Right-click the Help sheet's tab

Select 'View Code' from the menu and paste the code.

Alt-F11 to return to Excel.
************************************************************

Now paste the following sub into a standard module:

Code:

Public Sub RefuseToDelete()

MsgBox "This help sheet should not be deleted!", _
Buttons:=vbExclamation, _
Title:="Cannot Deelete Help!"

End Sub




Thanks for any help!,
jubi

Prevent Deletion Of Chart - Excel

View Content
Dear Forum Members,
.
Is that a way in vba to prevent users from deleting charts?

Prevent File Deletion - Excel

View Content
How do I keep a file from being deleted (deliberate or accidental) by other
users on a server but still have that file available to everyone to
use/modify?


Prevent Deletion Of A Group Of Cells - Excel

View Content
Lo',

Here's the project:
I'm working on a project that would detail a day's events for future record. It has a group of cells where users input the details of any particular event in question. This group of cells repeats multiple times straight down the sheet, covering a range of A to O. Each group is separated by a single, thin row (6 pixles high).

Current practice is to save a copy of the previous day's record and edit out all the unnecessary entries, while keeping the relevant ones.

Here's the problem:
Many different users will update this record. Not all of them know how to use a computer properly.

When they want to put in a new event, sometimes they will delete an old event by selecting one of the groups of cells, and deleting them outright. They don't select the rows, they select the cells. When they delete the cells, it messes with the formatting of the sheet.

What I think happens, is that excel moves the group of cells one row down, into the thin row, shrinking the bottom part of the group.

What I want to do:
I want to prevent the users from deleting the cells completely. They still need to be able edit the text in the cells, though.

If I can't prevent the outright deletion of the cells, maybe a suggestion to stop the resizing that happens?

I don't know VBA, and I'm still new to excel.

-John

Validation Needed To Prevent Deletion - Excel

View Content
In many of the cells where I have formulas entered, I have validated them to allow only whole numbers less than zero to prevent users from accidentally changing or deleting the formulas. This seems to work fine until someone hits delete, which allows the user to erase the formula and get away with it. I cannot use protection because then I can't merge cells, so I'm wondering if it's possible to use something else in the Validation field which won't allow anyone to change or delete the formula I worked so hard to come up with.

Prevent Deletion Of Excel File - Excel

View Content
Is there any way to prevent deletion of an Excel file through Microsoft
Explorer. The file is stored on a local drive.



Random Tutorials
Goal Seek Feature in Excel
(Intermediate)
FV() Find the Future Value of Cash Today
         -Savings/Retirement Plan Calculations

(Intermediate)
Remove #N/A Error Result from Empty VLOOKUP() Formulas
(Intermediate)
IF Statement Introduction & Using Nested IF's
(Easy)
HLOOKUP() Function - Introduction
(Intermediate)
AND(), OR(), and IF() Statements/Formulas
(Intermediate)
Submit Inquiry Here
  • Prices From $10
Name:*
E-mail:*
Request:*
The macro(s) on this page will be sent with the request.
Contact | Privacy Policy | Disclaimer
Copyright© 2012 TeachExcel.com