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
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
@superfb I think you left out some info needed.

Do you actually want to open all of the files that are able to be opened, or do you just want a list of files that exist and a list of files not found?
 
Upvote 0
@superfb I think you left out some info needed.

Do you actually want to open all of the files that are able to be opened, or do you just want a list of files that exist and a list of files not found?
Hi,

Apologise if i was vague.

I would like to open all the files in the list thanks
 
Upvote 0
P.s would also be good if the macro could go back to the original file where the macro was executed.
 
Upvote 0
You might be pushing the character limit of a message box, you will know if this happens because your message box will be cut off at the end, ie won't display the entire message.

Anyways, try the following:

VBA Code:
Sub CheckFileExists()
'
    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("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
    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
End Sub
 
Upvote 0
You might be pushing the character limit of a message box, you will know if this happens because your message box will be cut off at the end, ie won't display the entire message.

Anyways, try the following:

VBA Code:
Sub CheckFileExists()
'
    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("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
    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
End Sub
Hi Thank you very much for taking the time out to write me this code!
 
Upvote 0
You might be pushing the character limit of a message box, you will know if this happens because your message box will be cut off at the end, ie won't display the entire message.

Anyways, try the following:

VBA Code:
Sub CheckFileExists()
'
    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("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
    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
End Sub

It seems to be working fine, however, if that file doesnt exist it crashes the VBA code, would on error go to be implemented? not sure how to compute that into this code
 
Upvote 0
It seems to be working fine, however, if that file doesnt exist it crashes the VBA code, would on error go to be implemented? not sure how to compute that into this code

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
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,231
Members
449,091
Latest member
jeremy_bp001

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