The VBA code goes through all excel files in the folder and checks if some specific sheets exist. but if I run the code 2nd time, it creates new sheets. How can I make it stop?
Sub LoopAllExcelFilesInFolder()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim CurrentSheetName As String
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
myExtension = "*.xls*"
myFile = Dir(myPath & myExtension)
Do While myFile <> ""
Set wb = Workbooks.Open(Filename:=myPath & myFile)
DoEvents
CurrentSheetName = ActiveSheet.Name
Sheets.Add
On Error Resume Next
ActiveSheet.Name = "IGeneral"
CurrentSheetName = ActiveSheet.Name
Sheets(CurrentSheetName).Select
Sheets.Add
On Error Resume Next
ActiveSheet.Name = "IInput"
Sheets(CurrentSheetName).Select
CurrentSheetName = ActiveSheet.Name
Sheets.Add
On Error Resume Next
ActiveSheet.Name = "IResult"
Sheets(CurrentSheetName).Select
wb.Close SaveChanges:=True
DoEvents
myFile = Dir
Loop
MsgBox "Task Complete!"
ResetSettings:
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub