Code: Select all
Sub CheckCorruptFiles2()
Dim folderPath As String
Dim fileName As String
Dim wb As Workbook
' Set the folder path where your Excel files are located
folderPath = "C:\Users\Admin\Desktop\"
' Disable screen updating for faster execution
Application.ScreenUpdating = False
' Loop through each file in the folder
fileName = Dir(folderPath & "*.xls*")
Do While fileName <> ""
' Open the file in read-only mode
Set wb = Workbooks.Open(folderPath & fileName, ReadOnly:=True)
' Check if the file generates an error upon opening
If Err.Number <> 0 Then
Debug.Print "Corrupt File: " & fileName
Err.Clear
End If
' Close the file without saving changes
wb.Close SaveChanges:=False
' Move to the next file
fileName = Dir
Loop
' Enable screen updating
Application.ScreenUpdating = True
' Inform that the process is complete
MsgBox "Corrupt file check completed.", vbInformation
End Sub