Error Handling

maclachlan19

Board Regular
Joined
Jul 8, 2013
Messages
53
Hoping somebody can help, I'm a rookie in VBA (but learning).
I have the piece of code below and had 2 questions.

1. I would like to add code so that if the Path isn't available the spreadsheet doesn't give the user an error upon opening. This could happen if the user moves the file from the shared folder.

2. What does the Option Explicit do at the start of the code, is this required or should it be there.

Thanks


Option Explicit


Private Sub Workbook_Open()
Open ThisWorkbook.Path & "\old reports\logfiles\ELR Analysis.log" For Append As #1
Print #1 , Application.UserName, Now
Close #1
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Option Explicit means you must declare ALL variables
try
Code:
Private Sub Workbook_Open()
On Error GoTo handler:
Open ThisWorkbook.Path & "\old reports\logfiles\ELR Analysis.log" For Append As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , Application.UserName, Now
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
handler:
MsgBox "File Not Available"
End Sub
 
Upvote 0
I cut and paste your code but I now get the Message box every time I open the file, regardless if the log is working proper or if the path isn't valid. It seems like the On Error part isn't working like it should.
 
Upvote 0
Sorry, I'm in the car...

Code:
Private Sub Workbook_Open()
On Error GoTo errhandler:
Open ThisWorkbook.Path & "\old reports\logfiles\ELR Analysis.log" For Append As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , Application.UserName, Now
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
Exit Sub
errhandler:
MsgBox "File Not Available"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,743
Messages
6,180,686
Members
452,994
Latest member
Janick

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