Searching and Deleting Workbook Tab

SaintBobCat

New Member
Joined
Jul 17, 2007
Messages
26
Hi there

Can someone help me with some vba code that opens a workbook, searches for a tab with a specific name and deletes the tab if it is there then closes the workbook.

I'm sure this would have been answered before and i have had a search of previous posts without luck.

Cheers!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
This this (untested):-
Code:
Sub deleter()
    dim ws as worksheet
    for each ws in worksheets
        if ws.name="Insert Tab name here" then
            application.displayalerts=false
            ws.delete
            application.displayalerts=true
        endif
    next
end sub

This will search the open/active workbook for the specific name you enter.
 
Upvote 0
Just before we start plinking code, is it always the same workbook (same fullname) or is the user to browse for it?
 
Upvote 0
Try like this

Code:
Sub test()
Workbooks.Open Filename:="C:\test.xls)"
If WorksheetExists("Sheet1") Then
    Application.DisplayAlerts = False
    Sheets("Sheet1").Delete
    Application.DisplayAlerts = True
End If
ActiveWorkbook.Close savechanges:=True
End Sub



Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function
 
Upvote 0
try, Something like:
Rich (BB code):
Sub ImMissingMyBanana ()
Dim TabName as String
Dim i as Long
Dim SheetExists as Boolean
With Application
  .DisplayAlerts = False
  .ScreenUpdating = False
End With
Workbooks.Open Filename:="path and name of your file"
On Error Goto NotFound
TabName = InputBox("Enter the name of the worksheet you want to delete: ")
For i = 1 to Worksheets.count
  If Sheets(i).Name = TabName Then
    If MsgBox("Please confirm you want to delete " & TabName) = vbYes Then
      Sheets(TabName).Delete
      SheetExists = True
      Exit For
    End If
  End If
Next i
If SheetExists Then
  MsgBox "Sheet: " & TabName" & " has been deleted"
Else
  MsgBox "Sheet: " & TabName" & " does not exist in this workbook"
End If
With Application
  .DisplayAlerts = True
  .ScreenUpdating = True
End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top