Share via

Excel Macro loop through files and delete specific sheet

Anonymous
2017-05-09T09:18:52+00:00

Dear Gurus

With your help I have managed to put the macro below together.  Now I want to enter the sheet name to be deleted into a input box.  The code at the moment asks me the sheet name with every file it opens.  how do I change this to only enter it once?  

Thank you

Es

Sub LoopAllExcelFilesInFolderDeleteSpecificSheetadvisorDashboard()

     'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them

     'SOURCE: www.TheSpreadsheetGuru.com

     Dim myPath As String

     Dim myExtension As String

     Dim myFile As String

     Dim wb As Workbook

     Dim ws As Worksheet

     Dim myValue As Variant

     'Retrieve Target Folder Path From User

     With Application.FileDialog(msoFileDialogFolderPicker)

         .Title = "Select A Target Folder"

         If .Show Then

             myPath = .SelectedItems(1)

         Else

             Exit Sub

         End If

     End With

     If Right(myPath, 1) <> "" Then

         myPath = myPath & ""

     End If

   'Optimize Macro Speed

     Application.ScreenUpdating = False

     Application.EnableEvents = False

     Application.Calculation = xlCalculationManual

     'Target File Extension (must include wildcard "*")

     myExtension = "*.xls*"

     'Target Path with Ending Extension

     myFile = Dir(myPath & myExtension)

     'Loop through each Excel file in folder

     Do While myFile <> ""

         'Set variable equal to opened workbook

         Set wb = Workbooks.Open(FileName:=myPath & myFile)

         'Test whether sheet exists

         Set ws = Nothing

         On Error Resume Next

      myValue = InputBox("Sheet Name")

Set ws = wb.Worksheets(myValue)

         If ws Is Nothing Then

             'Sheet doesn't exist - close workbook without saving it

             wb.Close SaveChanges:=False

         Else

             'Delete sheet without displaying a warning

             Application.DisplayAlerts = False

             ws.Delete

             Application.DisplayAlerts = True

             'Save and Close Workbook

             wb.Close SaveChanges:=True

         End If

         'Get next file name

         myFile = Dir

     Loop

     'Message Box when tasks are completed

     MsgBox "Task Complete!", vbInformation

ResetSettings:

     'Reset Macro Optimization Settings

     Application.EnableEvents = True

     Application.Calculation = xlCalculationAutomatic

     Application.ScreenUpdating = True

 End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2017-05-09T10:18:38+00:00

    The code at the moment asks me the sheet name with every file it opens.  how do I change this to only enter it once?  

    Move the line with InputBox outside the loop (before the line with "Do").

    Andreas.

    Was this answer helpful?

    0 comments No comments