VBA to delete a tab only if it exists

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,342
Office Version
  1. 365
Platform
  1. Windows
How do I change this code so that if the workbook does not have a tab named: Material Ad Hoc that it skips the part where its copying and deleting?

Code:
Sub Macro3()
'
'********************************
'This is the section of the code that I want pass over if there isn't a tab named "Material Ad Hoc".  Then proceed and do the next section of the code.
'
    Sheets("Material Ad Hoc").Select
    Sheets("Material Ad Hoc").Copy After:=Sheets(9)
    Sheets("Material Ad Hoc").Select
    ActiveWindow.SelectedSheets.Delete


'*********************************

Dim ProposalReports As Workbook
    Dim CMCS As Variant


    Set ProposalReports = ThisWorkbook
    
    CMCS = Application.GetOpenFilename("Excel files (*.xls*), *.xls*")
    
    If CMCS <> False Then
        Workbooks.Open (CMCS)
        Set CMCS = ActiveWorkbook
        
        
      Sheets("CMCS Bill of Materials").Select
            Sheets("Material Ad Hoc").Copy Before:=ThisWorkbook.Sheets(2)
    
    End If

End Sub
Thank you
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try:
VBA Code:
Sub Macro3()
    If Evaluate("isref('" & "Material Ad Hoc" & "'!A1)") Then
        With Sheets("Material Ad Hoc")
            .Copy After:=Sheets(9)
            Application.DisplayAlerts = False
            .Delete
            Application.DisplayAlerts = True
        End With
    End If
    Dim ProposalReports As Workbook, CMCS As Variant
    Set ProposalReports = ThisWorkbook
    CMCS = Application.GetOpenFilename("Excel files (*.xls*), *.xls*")
    If CMCS <> False Then
        Workbooks.Open (CMCS)
        Set CMCS = ActiveWorkbook
        Sheets("CMCS Bill of Materials").Select
        Sheets("Material Ad Hoc").Copy Before:=ThisWorkbook.Sheets(2)
    End If
End Sub
 
Upvote 0
How about
VBA Code:
    If Evaluate("isref('Material Ad Hoc'!A1)") Then
      Sheets("Material Ad Hoc").Select
      Sheets("Material Ad Hoc").Copy After:=Sheets(9)
      Sheets("Material Ad Hoc").Select
      ActiveWindow.SelectedSheets.Delete
    End If
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,835
Members
449,192
Latest member
mcgeeaudrey

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