Provide an error message in macro if file not found.

kylesom

New Member
Joined
Aug 5, 2009
Messages
37
I have a bit of VBA that copys a range of data in one worksheet, opens another worksheet at a fixed network location, and pastes the data.

I've had a couple of instances where users have tried to use the macro outside of the network, and so get VBA errors.

How can I add a check at the start of the code to look to see if the file location exists, and return an error message if it doesnt?

The bit of code I use to open the network worksheet, paste data in, and close it is as follows:

Code:
'Specify Database Location!
        Set JDB = Workbooks.Open("R:\_Admin\Timesheets\Database\Timesheet Swap File {DO NOT MOVE}.xls")
    
            With JDB.Worksheets("Input")
             .Range("A" & .Range("A" & Rows.Count).End(xlUp).Offset(1).Row).PasteSpecial Paste:=xlValues
            End With
            
        JDB.Save
        JDB.Close
        
        Set JDB = Nothing
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Include the lines in red to your macro and try:
Rich (BB code):
'Specify Database Location!
On Error Resume Next
        Set JDB = Workbooks.Open("R:\_Admin\Timesheets\Database\Timesheet Swap File {DO NOT MOVE}.xls")
If JDB Is Nothing Then
  MsgBox "Incorrect location, file not found. Macro stopping"
  Exit Sub
End If
 
            With JDB.Worksheets("Input")
             .Range("A" & .Range("A" & Rows.Count).End(xlUp).Offset(1).Row).PasteSpecial Paste:=xlValues
            End With
 
        JDB.Close savechanges:=True 
        Set JDB = Nothing
 
Upvote 0
You can test for the presence of a file:-
Code:
If Dir("R:\_Admin\Timesheets\Database\Timesheet Swap File {DO NOT MOVE}.xls")="" Then
[COLOR=black]  MsgBox "Incorrect location, file not found. Macro stopping"
  Exit Sub
End If
[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,463
Members
452,915
Latest member
hannnahheileen

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