vba 如何删除某个工作表后的工作表?

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

How to delete the sheets after a certain sheet?

excelvbaexcel-vba

提问by lakesh

I have worksheet in my workbook as Test. There are other worksheets as well. I want to delete the worksheets that appear on the right of the worksheet called Test.

我的工作簿中有工作表作为Test。还有其他工作表。我想删除出现在名为 Test 的工作表右侧的工作表。

How do I do that in VBA? Need some help on this.

我如何在 VBA 中做到这一点?需要一些帮助。

Edit:

编辑:

Set test= ThisWorkbook.Worksheets("Test")
For i = Sheets.Count To (test.Index + 1) Step -1
    Delete Sheets(i)
Next

回答by Gary's Student

Consider:

考虑:

Sub SheetKiller()
    Dim i As Long
    Dim j As Long
    j = 0
    For i = 1 To Sheets.Count
        If Sheets(i).Name = "Test" Then
            j = i
        End If
    Next i

    If j = 0 Or j = Sheets.Count Then Exit Sub

    Application.DisplayAlerts = False
        For i = Sheets.Count To j + 1 Step -1
            Sheets(i).Delete
        Next i
    Application.DisplayAlerts = True
End Sub

回答by lakesh

Taking an alternate approach to what has already been offered, if you had 10 sheets (worksheets..?) and wanted to remove everything after the Testworksheet, simply continually delete sheet(Sheets("test").Index + 1)until sheets.count is less than Sheets("test").Index +1.

对已经提供的内容采用另一种方法,如果您有 10 张工作表(工作表..?)并想在测试工作表之后删除所有内容,只需不断删除,sheet(Sheets("test").Index + 1)直到 sheet.count 小于 Sheets("test").Index +1。

Application.DisplayAlerts = False   'take off the training wheels
Do While Sheets.Count > Sheets("test").Index
    Sheets(Sheets("test").Index + 1).Delete
Loop
Application.DisplayAlerts = True