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

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.
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.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,042
Members
449,063
Latest member
ak94

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