简体   繁体   中英

Delete a specific worksheet from .xlsm file

Trying to delete a worksheet using the following code. Throwing an "Object required" error on the delete line. I have tried many variations on this line, ie, s.delete, sheets.s.delete, worksheets.delete, etc.

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

Try this code :

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

How about:

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
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM