You can't run any code if macros are disabled, they're disabled =)
What you can do, is show some "macros are disabled" sheet by default, and make sure the workbook is always saved with that sheet active.
Then in the Workbook.Open
event handler ( Private Sub Workbook_Open
in ThisWorkbook
), hide the "macros are disabled" sheet, and make the other ones visible.
Something like this (just for the concept - this is untested air-code), where MacrosDisabledSheet
is the code name of your "macros are disabled, please enable macros to use this workbook" sheet - this should hide all sheets except the "macros are disabled" sheet on save, and then unhide all sheets (and hide the "macros are disabled" sheet) once macros are enabled: or when the workbook is opened with macros enabled:
Private Sub Workbook_Open()
'when opened with macros enabled, runs on open.
'when opened with macros disabled, runs when macros are enabled.
ShowAllWorksheets
ShowMacrosDisabledSheet hide:=True
End Sub
Private Sub Workbook_BeforeSave()
'always save with the "macros disabled" sheet active/visible
ShowAllWorksheets hide:=True
ShowMacrosDisabledSheet
End Sub
Private Sub ShowAllWorksheets(Optional ByVal hide As Boolean = False)
Dim sheet As Worksheet
For Each sheet In ThisWorkbook.Worksheets
If Not sheet Is MacrosDisabledSheet Then
If hide Then
If sheet.Visible <> xlSheetHidden Then sheet.Visible = xlSheetHidden
Else
If sheet.Visible <> xlSheetVisible Then sheet.Visible = xlSheetVisible
End If
End If
Next
End Sub
Private Sub ShowMacrosDisabledSheet(Optional ByVal hide As Boolean = False)
MacrosDisabledSheet.Visible = IIf(hide, xlSheetVeryHidden, xlSheetVisible)
End Sub
If you need the workbook to still be usable with macros disabled, then just keep in mind that the state of the workbook when you save it, will be the state it opens in - whether macros are enabled or not - so because you cannot know if the workbook will next be opened with macros enabled or disabled, you have to prepare the workbook to be in a "macros are disabled" state on save.
Excel keeps its settings in the Windows registry. Disable all with notification
| Disable all except digitally signed macros
| Disable all without notification
| Enable all macros (not recommended)
, which once applied, will be translated into the values 2
| 3
| 4
| 1
for the DWORD
value VBAWarnings
under the hive:
HKEY_CURRENT_USER\software\policies\microsoft\office\15.0\excel\security
where 15.0 stands for the Excel version installed on the system.
Value 1: Enable All macros
Value 2: Disable all macros with notification
Value 3: Disable all macros except those digitally signed
Value 4: Disable all without notification
You can use the following code in VBA to read and write the Windows registry keys:
'reads the value for the registry key i_RegKey
'if the key cannot be found, the return value is ""
Function RegKeyRead(i_RegKey As String) As String
Dim myWS As Object
On Error Resume Next
'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'read key from registry
RegKeyRead = myWS.RegRead(i_RegKey)
End Function
'sets the registry key i_RegKey to the
'value i_Value with type i_Type
'if i_Type is omitted, the value will be saved as string
'if i_RegKey wasn't found, a new registry key will be created
' change REG_DWORD to the correct key type
Sub RegKeySave(i_RegKey As String, _
i_Value As String, _
Optional i_Type As String = "REG_DWORD")
Dim myWS As Object
'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'write registry key
myWS.RegWrite i_RegKey, i_Value, i_Type
End Sub
'returns True if the registry key i_RegKey was found
'and False if not
Function RegKeyExists(i_RegKey As String) As Boolean
Dim myWS As Object
On Error GoTo ErrorHandler
'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'try to read the registry key
myWS.RegRead i_RegKey
'key was found
RegKeyExists = True
Exit Function
ErrorHandler:
'key was not found
RegKeyExists = False
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.