Skip Corrupt Workbooks In a loop

litrainer

New Member
Joined
Oct 22, 2010
Messages
5
I have a list of 34,000 *.xls files, some corrupt, some not, no way to tell.
I need a way to loop through them, ignoring "this file cannot be opened" error messages, open the valid ones, run my code, close the workbook and move on to the next.

I have a workbook listing all the filenames if this is a good place to start. Have no clue to loop through workbooks let alone skip over the corrupt ones
 
Maybe:
Code:
Option Explicit
 
Sub NewTest_20101101a()
Dim wb As Workbook
Dim CurrentFolder As String:    CurrentFolder = "special folder name"
Dim wbNames As Range:           Set wbNames = Range("GroupList[Group1]")
Dim Nm As Range
 
Application.DisplayAlerts = False
On Error Resume Next
 
For Each Nm In wbNames
    Set wb = Workbooks.Open("W:\directory\" _
         & CurrentFolder & "\" & Nm & ".xlsx")
 
    If Not wb Is Nothing Then
        Call OpenFile
        wb.Close False
    End If
Next Nm
 
End Sub
In another reply I stated with a few minor changes this works great, and thank you.
I've run into an issue on my end though. In one of the folders (where the macro looks for files named in the "group" list) it doesn't have one or more of the files named in the given list. What sort of error control should I use for this? thank you again,

-- g
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
The code should simply move on if it encounters a value in the group that doesn't result in a wb being opened.

We've turned on error resume and after we attempt to open a wb name, we test to see if it worked.

Hmm, maybe this tiny addition:
Rich (BB code):
Option Explicit

Sub NewTest_20101101a()
Dim wb As Workbook
Dim CurrentFolder As String:    CurrentFolder = "special folder name"
Dim wbNames As Range:           Set wbNames = Range("GroupList[Group1]")
Dim Nm As Range

Application.DisplayAlerts = False
On Error Resume Next

For Each Nm In wbNames
    Set wb = Workbooks.Open("W:\directory\" _
         & CurrentFolder & "\" & Nm & ".xlsx")

    If Not wb Is Nothing Then
        Call OpenFile
        wb.Close False
        Set wb = Nothing
    End If
Next Nm

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,663
Messages
6,126,097
Members
449,291
Latest member
atfoley16

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