VBA Help

superfb

Active Member
Joined
Oct 5, 2011
Messages
251
Office Version
  1. 2007
Platform
  1. Windows
Hi all,

I was wondering if someone could kindly help me with a code to open a listen of files listed in a column. Let's say A5 and downwards.

Number of files can change, however once the macro opens all the files I have a message box at the end telling me what files opened.

Equally if that file doesn't exists, can the macro carry on, but at the end give me a list of filenames that did open and didn't?

Thanks
 
I'm not sure how that could happen because the code does a check to see if the file exists, if it doesn't exist, it doesn't try to open it.

Here is another version you can try that would handle files that do exist but cause an error:

VBA Code:
Sub CheckFileExistsV2()
'
    Dim Cel                     As Range
    Dim FileExistsErrorString   As String
    Dim FileExistsValidString   As String
'
    FileExistsErrorString = "Files that did not open:" & vbCrLf                         ' Initiate string that saves files that didn't open
    FileExistsValidString = "Files that opened successfully:" & vbCrLf                  ' Initiate string that saves files that opened
'
    On Error GoTo ErrorHandler
'
    For Each Cel In Range("A5:A" & Range("A" & Rows.Count).End(xlUp).Row)               ' For every cell in the range of files to be opened ...
        If Dir(Cel.Value) = "" Then                                                     '   If File doesn't exist ...
            FileExistsErrorString = FileExistsErrorString & Cel.Value & vbCrLf          '       Append the FileName to the FileExistsErrorString
        Else                                                                            '   If File exists ...
            Workbooks.Open Cel.Value                                                    '       Open the file
            FileExistsValidString = FileExistsValidString & Cel.Value & vbCrLf          '       Append the FileName to the FileExistsValidString
        End If
CheckNext:
    Next
'
    MsgBox FileExistsValidString & vbCrLf & vbCrLf & FileExistsErrorString              ' Display message box of files that opened and didn't open
'
    ThisWorkbook.Sheets(1).Activate                                                     ' Return to This workbook
    Exit Sub
'
ErrorHandler:
    Err.Clear
    FileExistsErrorString = FileExistsErrorString & Cel.Value & vbCrLf                  ' Append the FileName to the FileExistsErrorString
    GoTo CheckNext
End Sub
Apoligise in the delay in the reply

i just gave this macro a run, and i got a type 13 error message, i believe for some weird reason when it opens the last file, it causes an error? looking at the code this shouldnt really happen?
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try moving the on error goto line to below the For each cel line
 
Upvote 0
Try moving the on error goto line to below the For each cel line
it crashed on the same line...

VBA Code:
 For Each Cel In Range("Q2:Q" & Range("Q" & Rows.Count).End(xlUp).Row)               ' For every cell in the range of files to be opened ...
             On Error GoTo ErrorHandler
        If Dir(Cel.Value) = "" Then

I think after all the Spreadsheets have opened, and the next row is empty...it crashes

whereas the code says if the row is empty then a message box appears?
 
Last edited:
Upvote 0
I have no idea how you can get an error to pop up when there is error handling code.

Can you post what your column Q looks like?
 
Upvote 0
Thank you @Fluff. There was a misunderstanding on my part. I thought Err.Clear cleared the error flag, upon further review, it just clears the text.

Now that the misconception has been cleared up ... here is the revised code:

VBA Code:
Sub CheckFileExistsV2_5()
'
    Dim Cel                     As Range
    Dim FileExistsErrorString   As String
    Dim FileExistsValidString   As String
'
    FileExistsErrorString = "Files that did not open:" & vbCrLf                         ' Initiate string that saves files that didn't open
    FileExistsValidString = "Files that opened successfully:" & vbCrLf                  ' Initiate string that saves files that opened
'
    For Each Cel In Range("Q2:Q" & Range("Q" & Rows.Count).End(xlUp).Row)               ' For every cell in the range of files to be opened ...
        If Dir(Cel.Value) = "" Then                                                     '   If File doesn't exist ...
            FileExistsErrorString = FileExistsErrorString & Cel.Value & vbCrLf          '       Append the FileName to the FileExistsErrorString
        Else                                                                            '   If File exists ...
            Workbooks.Open Cel.Value                                                    '       Open the file
            FileExistsValidString = FileExistsValidString & Cel.Value & vbCrLf          '       Append the FileName to the FileExistsValidString
        End If
CheckNext:
    Next
'
    MsgBox FileExistsValidString & vbCrLf & vbCrLf & FileExistsErrorString              ' Display message box of files that opened and didn't open
'
    ThisWorkbook.Sheets(1).Activate                                                     ' Return to This workbook
    Exit Sub
'
ErrorHandler:
    On Error GoTo -1                                                                    ' Clear the error flag
    On Error GoTo ErrorHandler                                                          ' Reset error handler
    FileExistsErrorString = FileExistsErrorString & Cel.Value & vbCrLf                  ' Append the FileName to the FileExistsErrorString
    GoTo CheckNext                                                                      ' Return to code
End Sub
 
Upvote 0
Personally I would use
VBA Code:
ErrorHandler:
    FileExistsErrorString = FileExistsErrorString & Cel.Value & vbCrLf                  ' Append the FileName to the FileExistsErrorString
    Resume CheckNext                                                                      ' Return to code
 
Upvote 0
I would have also, if I knew such an option existed. :rolleyes: With the use of Resume, it clears the error flag also. Nice!
Thank you once again @Fluff.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,432
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