Delete or rename an Excel file if it does not contain a sheet with a specific name

Ovisele

New Member
Joined
Apr 28, 2015
Messages
30
Hi Guys,

Please indulge me in the following problem. I need to parse over 700 files in order to establish if they contain or not a specific sheet "Corrective Saccades". If this particular sheet is present in the file, I would like to rename that file, or rename/delete the files that are not containing that specific sheet - whatever is easier.

An example file is here: Corrective Saccades.xlsx

Please see my piece of code below. Many thanks in advance!

VBA Code:
Sub LoopFiles()

    Dim xFd As FileDialog
    Dim xFdItem As Variant
    Dim xFileName As String
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    If xFd.Show = -1 Then
        xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
        xFileName = Dir(xFdItem & "*.xls*")
        Do While xFileName <> ""
            With Workbooks.Open(xFdItem & xFileName)
                'your code here
   For Each xWs In Application.ActiveWorkbook.Worksheets
        If xWs.Name = "Corrective Saccades" Then
           [B] WHAT DO I DO NEXT?[/B]
        End If
    Next
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    ActiveWorkbook.Save
    ActiveWorkbook.Close
            End With
            xFileName = Dir
        Loop
    End If
End Sub
 
If you want to delete the files try
VBA Code:
Sub LoopFiles()

   Dim xFd As Object
   Dim xFdItem As Variant
   Dim xFileName As String
 
   Set xFd = Application.FileDialog(4)
   If xFd.Show = -1 Then
      xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
   End If
   xFileName = Dir(xFdItem & "*.xls*")
   Application.ScreenUpdating = False
   Application.DisplayAlerts = False
   Do While xFileName <> ""
      With Workbooks.Open(xFdItem & xFileName)
         If ShtExists("Corrective Saccades", Workbooks(.Name)) Then
            .Close False
         Else
            .Close False
            Kill xFdItem & xFileName
         End If
         Application.DisplayAlerts = False
      End With
      xFileName = Dir
   Loop
End Sub
Public Function ShtExists(shtName As String, Optional Wbk As Workbook) As Boolean
    If Wbk Is Nothing Then Set Wbk = ActiveWorkbook
    On Error Resume Next
    ShtExists = (LCase(Wbk.Sheets(shtName).Name) = LCase(shtName))
    On Error GoTo 0
End Function
This also works! Thank you guys, you are brilliant. Unfortunately, I cannot mark both answers as solutions but Kudos to you both!
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

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