vba 删除工作表并使用相同的工作表名称重新创建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21617020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:48:18  来源:igfitidea点击:

Delete a worksheet and recreate with the same sheet name

excelvbaexcel-vbaaddworksheet

提问by Adrian Gornall

I'm currently trying to delete a worksheet and auto create a new worksheet with the same name.

我目前正在尝试删除工作表并自动创建一个具有相同名称的新工作表。

I'm using the below code. However, when i run the code the windows pop up appears asking me to confirm deletion, i want to prevent this and just delete and replace with a new sheet. I want to avoid Send-keys for this.

我正在使用下面的代码。但是,当我运行代码时,会弹出窗口要求我确认删除,我想防止这种情况发生,只需删除并替换为新工作表即可。我想为此避免使用发送键。

ThisWorkbook.Sheets("Process Map").delete
ThisWorkbook.Sheets.Add.Name = "Process Map"

回答by AER

Try setting the DisplayAlerts to False (then back to true if that is what you want as the default setting.

尝试将 DisplayAlerts 设置为 False(如果这是您想要的默认设置,则返回 true。

Application.DisplayAlerts = False
On Error Resume Next 
ThisWorkBook.Sheets("Process Map").Delete 
On Error Goto 0 
Application.DisplayAlerts = True
ThisWorkbook.Sheets.Add.Name = "Process Map" 

Or without error handling:

或者没有错误处理:

Application.DisplayAlerts = False
ThisWorkBook.Sheets("Process Map").Delete 
Application.DisplayAlerts = True
ThisWorkbook.Sheets.Add.Name = "Process Map"