Changed File to Add-In, Now Code Doesn't Work

eliz

New Member
Joined
Sep 12, 2006
Messages
46
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 stored 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.

Any suggestions?

Thanks,
Eliz

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)


'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()

'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
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
Add-ins are just that they add-into a workbook, the master workbook is not the add-in, the add-in is a tool used by the master workbook. So, you will have problems working on sheets not part of your add-in.

You can convert whatever you are doing in the application you are calling an add-in, to UserForms and add a Button to the toolbar of the master application that uses the addin to open the add-in Utility.
 

Forum statistics

Threads
1,141,756
Messages
5,708,329
Members
421,565
Latest member
Lastadiego

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
Top