gpt4 book ai didi

excel - 删除工作表

转载 作者:行者123 更新时间:2023-12-03 01:57:49 27 4
gpt4 key购买 nike

我正在尝试运行删除所有工作表(第三个)的代码。

下面的代码会导致语法错误:

Sub Delete_Sheets()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet1", "LBO", "Factset codes" Then ws.Delete
Next
Application.DisplayAlerts = True
End Sub

最佳答案

您不能像这样将所有“要检查的名称”放在一行中。试试这个(替换你的 If block ):

Select Case ws.Name 
Case "Sheet1", "LBO", "Factset codes"' Do Nothing here
Case Else: ws.Delete
End Select

编辑(添加代码以使其静默删除):

Application.DisplayAlerts = False
Select Case ws.Name
Case "Sheet1", "LBO", "Factset codes" ' Do Nothing here
Case Else
If Worksheets.Count > 1 Then ws.Delete
End Select
Application.DisplayAlerts = True

关于excel - 删除工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34983205/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com