Does VBA only let you protect the entire Project?

eliz

New Member
Joined
Sep 12, 2006
Messages
46
I have written code for another user, I don't want them to be able to access/edit (they could view it) the code. But what if they write their own macros?
It seems it only allows protection on the entire file, not on individual procedures? Is this correct?

Thanks.
 

Some videos you may like

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Why not protect it and save as an Add-in (File > Save As).
 

eliz

New Member
Joined
Sep 12, 2006
Messages
46
Can you please explain in more depth.
I've used a couple of built-in Add-ins (i.e. Solver, Template Wizard), but I've never created an Add-In before. BTW I'm using version 2003.

Thanks,
Eliz
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
If you just place the macro code in modules in a workbook, protect the project, then File > Save as and select the last option Add-in *.xla.

Copy the .xla file to the other user's PC, open Excel, Tools > Add-ins and browse to select the .xla file.

They won't be able to view your code but will be able to view their own.
 

eliz

New Member
Joined
Sep 12, 2006
Messages
46

ADVERTISEMENT

Does VBA only let you protect the entire Project

Thank you,

I will give that a try.
Eliz
 

eliz

New Member
Joined
Sep 12, 2006
Messages
46
I Saved File as an Add-In Now Code doesn't Work

Hi VoG II

I tried creating an Add-In to store my code.
Now my code doesn't seem to work.

My code loops through each sheet and therefore was being store in "ThisWorkbook" and is triggered on the Open & Save events for the file.

It wasn't being stored in a separate module.

It has to work for a variety of different files for different users, so I want it to remain pretty general.

Thanks,
Eliz

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
' for SoX high-risk spreadsheets, Elizabeth McLeod Sep-14-06

'reduce screen flicker
Application.ScreenUpdating = False

'declare variables
Dim SH As Worksheet
Dim rng As Range

On Error Resume Next

'loop through each sheet, lock formulas, protect sheets
For Each SH In Worksheets
SH.Unprotect

With SH.UsedRange
.Locked = False
Set rng = Nothing
Set rng = .SpecialCells(xlCellTypeFormulas)
If Not rng Is Nothing Then
'rng.Font.ColorIndex = 7 'optional changes color of all formulas
rng.Locked = True
End If
End With

' allow users to still insert rows with protection on
SH.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowInsertingColumns:=True, AllowInsertingRows:=True

Next SH

Application.ScreenUpdating = True

End Sub
Private Sub Workbook_Open()
' for SoX high-risk spreadsheets, Elizabeth McLeod Sep-14-06

'reduce screen flicker
Application.ScreenUpdating = False
Application.CommandBars("Protection").Visible = True

'declare variables
Dim SH As Worksheet
Dim rng As Range

On Error Resume Next

'loop through each sheet, lock formulas, protect sheets
For Each SH In Worksheets
SH.Unprotect

With SH.UsedRange
.Locked = False
Set rng = Nothing
Set rng = .SpecialCells(xlCellTypeFormulas)
If Not rng Is Nothing Then
'rng.Font.ColorIndex = 7 'optional changes color of all formulas
rng.Locked = True
End If
End With

' allow users to still insert rows with protection on
SH.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowInsertingColumns:=True, AllowInsertingRows:=True

Next SH

Application.ScreenUpdating = True

End Sub
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Ah, I was assuming the code was in a standard module, not in the workbook's code. I don't think that an Add-in will work in your situation. sorry for leading you down the wrong path.
 

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
Yes, you can password protect individual modules in the project. Insert a Standard Module, Using the VBA Editor ToolBar: Insert - Module [You can rename it if you want] Then add your code to it, adjust any Sheet Module code to work in the Standard Module.

Then from the Project Manager [the list of Modules in your Workbook in the Editor] Right-Click that Module and Select "VBAProjectProperties..." and then the Protection tab. Now your user can add or edit any other modules and yours will be protected!

For ThisWorkbook and Sheet Module Events Use the Events but move the code to your protected Module and add to a Sub. Then from the Event call the Sub in your protected Module. The code will run from the Event through the Call and the User will not be able to see the actual code.

You could also protect the ThisWorkbook Module if you like, then the User will only be able to add Standard modules, Forms, Classes and use any Sheet Module that you did not protect!
 

Watch MrExcel Video

Forum statistics

Threads
1,111,596
Messages
5,541,154
Members
410,543
Latest member
ExcelGlenn
Top