使用 VBA 在 Excel 中删除工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31475376/
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-08 09:47:06  来源:igfitidea点击:

Delete worksheet in Excel using VBA

excelvbaexcel-vba

提问by Clauric

I have a macros that generates a number of workbooks. I would like the macros, at the start of the run, to check if the file contains 2 spreadsheets, and delete them if they exist.

我有一个生成许多工作簿的宏。我希望宏在运行开始时检查文件是否包含 2 个电子表格,如果存在则将其删除。

The code I tried was:

我试过的代码是:

If Sheet.Name = "ID Sheet" Then
    Application.DisplayAlerts = False
    Sheet.Delete
    Application.DisplayAlerts = True

End If

If Sheet.Name = "Summary" Then
    Application.DisplayAlerts = False
    Sheet.Delete
    Application.DisplayAlerts = True
End If

This code is returning an error:

此代码返回错误:

run time error #424, object required.

运行时错误 #424,需要对象。

I probably have the wrong formatting, but if there is an easier way to do this, it would be very useful.

我可能有错误的格式,但如果有更简单的方法来做到这一点,那将非常有用。

回答by Gary's Student

Consider:

考虑:

Sub SheetKiller()
    Dim s As Worksheet, t As String
    Dim i As Long, K As Long
    K = Sheets.Count

    For i = K To 1 Step -1
        t = Sheets(i).Name
        If t = "ID Sheet" Or t = "Summary" Then
            Application.DisplayAlerts = False
                Sheets(i).Delete
            Application.DisplayAlerts = True
        End If
    Next i
End Sub

NOTE:

笔记:

Because we are deleting, we run the loop backwards.

因为我们正在删除,所以我们向后运行循环

回答by R.Katnaan

Try this code:

试试这个代码:

For Each aSheet In Worksheets

    Select Case aSheet.Name

        Case "ID Sheet", "Summary"
            Application.DisplayAlerts = False
            aSheet.Delete
            Application.DisplayAlerts = True

    End Select

Next aSheet

回答by dee

You could use On Error Resume Nextthen there is no need to loop through all the sheets in the workbook.

您可以使用On Error Resume Next然后无需遍历工作簿中的所有工作表。

With On Error Resume Nextthe errors are not propagated, but are suppressed instead. So here when the sheets does't exist or when for any reason can't be deleted, nothing happens. It is like when you would say : delete this sheets, and if it fails I don't care.Excel is supposed to find the sheet, you will not do any searching.

随着On Error Resume Next误差不传播,而是被抑制。因此,当工作表不存在或由于任何原因无法删除时,没有任何反应。就像你会说:删除这张表,如果它失败了我不在乎。Excel 应该找到工作表,你不会做任何搜索。

Note: When the workbook would contain only those two sheets, then only the first sheet will be deleted.

注意:当工作簿仅包含这两个工作表时,只会删除第一张工作表。

Dim book
Dim sht as Worksheet

set book= Workbooks("SomeBook.xlsx")

On Error Resume Next

Application.DisplayAlerts=False 

Set sht = book.Worksheets("ID Sheet")
sht.Delete

Set sht = book.Worksheets("Summary")
sht.Delete

Application.DisplayAlerts=True 

On Error GoTo 0

回答by user7495253

Worksheets("Sheet1").Delete
Worksheets("Sheet2").Delete

回答by Luuklag

try this within your if statements:

在你的 if 语句中试试这个:

Application.DisplayAlerts = False
Worksheets(“Sheetname”).Delete
Application.DisplayAlerts = True