You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

模块中同名Workbook_Open子过程问题:如何重命名使代码正常运行

Fixing Duplicate Workbook_Open Procedures in VBA

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:

  1. Rename each duplicate sub to a unique, descriptive name

    • For your first sub (which navigates to the Guide sheet), something like SetupGuideNavigation works great.
    • For your second sub (which disables keyboard shortcuts), DisableRestrictedShortcuts is a clear choice.
      You can use any unique names you prefer—just pick ones that reflect what the code does for readability.
  2. Keep a single Workbook_Open event 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:07:28