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

如何阻止Excel在新建工作表时自动切换至新工作表?

阻止新建工作表时自动跳转的VBA解决方案

要避免新建工作表时自动跳转,核心思路是先记录当前激活的工作表,创建新表后再切回原工作表。修改你的AddSheet代码即可实现:

Option Explicit

Public sheetName As Variant

Sub AddSheet()
    On Error Resume Next
    Dim currentSheet As Worksheet
    ' 保存当前激活的工作表
    Set currentSheet = ActiveSheet
    
    sheetName = InputBox("New Sheet Name", "Prototype 01")
    If sheetName = "" Then
        MsgBox "Sheet name cannot be empty!"
        Exit Sub
    End If
    
    ' 创建新表
    Sheets.Add(After:=Sheets(Sheets.Count)).Name = sheetName
    
    ' 切回原来的工作表
    currentSheet.Activate
    
    MsgBox "" & sheetName & " was successfully created!"
End Sub

Sub DeleteSheet()
    On Error Resume Next
    sheetName = InputBox("Sheet Name", "Prototype 01")
    If sheetName = "" Then Exit Sub
    Sheets(sheetName).Delete
    MsgBox """" & sheetName & """ was successfully removed!"
End Sub

关键说明

  • 新增currentSheet变量,在创建新表前保存当前活动工作表的引用。
  • 新表创建完成后,调用currentSheet.Activate强制切回原工作表,覆盖Sheets.Add默认的激活行为。
  • 保留你原有的错误处理逻辑和弹窗提示,不影响原功能的使用。

内容的提问来源于stack exchange,提问作者HellKat02

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 20:05:12