从.xlsm文件中删除特定工作表

时间:2018-01-23 16:26:02

标签: excel-vba vba excel

尝试使用以下代码删除工作表。在删除行上抛出“需要对象”错误。我在这一行上尝试了很多变种,例如s.delete,sheets.s.lelete,worksheets.delete等。

Dim s As Worksheet
'Look for existing sheets named "For Export
'If found, delete existing sheet
For Each s In ActiveWorkbook.Sheets
    If s.Name = "For Export" Then
        Application.DisplayAlerts = False
        Workbook.Worksheets.Item(s.Name).Delete        
    End If
Next s

3 个答案:

答案 0 :(得分:1)

试试这段代码:

Sub deleteWorksheet()
    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 = "For Export" Then
                Application.DisplayAlerts = False
                Sheets(i).Delete
                Application.DisplayAlerts = True
        End If
    Next i
End Sub

答案 1 :(得分:1)

怎么样:

Sub poiuyt()
    Dim s As Worksheet
    For Each s In ActiveWorkbook.Sheets
      If s.Name = "For Export" Then
        Application.DisplayAlerts = False
            s.Delete
        Application.DisplayAlerts = True
      End If
    Next s
End Sub

答案 2 :(得分:0)

For Each s In ActiveWorkbook.Sheets
        If s.Name = ws_name Then
            Application.DisplayAlerts = False
            Sheets(ws_name).Delete
            Application.DisplayAlerts = True
            Exit For
        End If
Next s