模块中同名Workbook_Open子过程问题:如何重命名使代码正常运行
Hey there! The core problem here is that you can’t have two Workbook_Open event procedures in the same module (especially the ThisWorkbook module, where workbook-level events reside). VBA can’t resolve duplicate procedure names, so it won’t run either of them properly.
Here’s the straightforward fix:
Rename each duplicate sub to a unique, descriptive name
- For your first sub (which navigates to the Guide sheet), something like
SetupGuideNavigationworks great. - For your second sub (which disables keyboard shortcuts),
DisableRestrictedShortcutsis a clear choice.
You can use any unique names you prefer—just pick ones that reflect what the code does for readability.
- For your first sub (which navigates to the Guide sheet), something like
Keep a single
Workbook_Openevent that calls both renamed subs
This way, when the workbook opens, both sets of code execute in the order you specify.
Revised Code Example (for the ThisWorkbook module):
Private Sub Workbook_Open() ' Execute both custom procedures in sequence SetupGuideNavigation DisableRestrictedShortcuts End Sub Private Sub SetupGuideNavigation() Sheets("Guide").Select ActiveSheet.Range("A9").Select End Sub Private Sub DisableRestrictedShortcuts() Application.OnKey "%{F11}", "DisableAltF11" Application.OnKey "%{F8}", "DisableAltF8" End Sub
A quick note: Make sure the DisableAltF11 and DisableAltF8 procedures (the ones you’re mapping to the shortcuts) are stored in a standard VBA module (not ThisWorkbook), so they’re accessible to the Application.OnKey calls.
内容的提问来源于stack exchange,提问作者Kashish Jain

