Reputation: 215
I have a Sub WorkSheet_Change(ByVal Target As Range)
which i want to automatically run for all users. The problems is these users barely know or bother to enable macro when entering data. Is there a way to force macro to automatically run on this workbook? I have seen an article about an event called Workbook_Open ()
but i have no idea on how to use it. Any help?
Upvotes: 0
Views: 12089
Reputation: 639
For security reasons, you can't force a workbook to enable macros without explicit input from the user.
What you can do though, is set all the sheets the user would need to hidden. Then create a visible sheet with just a textbox or something that says "Please enable macros to continue." Then use the Workbook_Open
event to hide that sheet and unhide the other sheets.
Additionally, use the Workbook_BeforeSave
event to re-hide the sheets when the user saves the spreadsheet, so the check has to be performed every time.
Additional tip: When hiding sheets using VBA, try to use xlSheetVeryHidden
versus xlSheetHidden
. This means the users won't be able to unhide them from excel like a normal hidden sheet (so you can avoid a stubborn user using that loophole).
EDIT: Here's some sample code, with made-up sheet names of course:
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
Worksheets("Data1").Visible = xlSheetVisible
Worksheets("Data2").Visible = xlSheetVisible
Worksheets("Welcome").Visible = xlSheetVeryHidden
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Worksheets("Welcome").Visible = xlSheetVisible
Worksheets("Data1").Visible = xlSheetVeryHidden
Worksheets("Data2").Visible = xlSheetVeryHidden
End Sub
Private Sub Workbook_Open()
Worksheets("Data1").Visible = xlSheetVisible
Worksheets("Data2").Visible = xlSheetVisible
Worksheets("Welcome").Visible = xlSheetVeryHidden
End Sub
Upvotes: 2