Display certain error message

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,362
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I want to display a specific error message upon a set error being encountered. If error 53, or file not found appears, I will know the cause and solution.

I have some allocation sheet files that are stored on a network and I have some code within the ThisWorkbook module of those files. The code lets the user know if another user already has it open when the allocation file is opened.

VBA Code:
Private Sub Workbook_Open()
Dim file1 As Integer
Dim strLine As String
file1 = FreeFile
    If Not ActiveWorkbook.ReadOnly = True Then
        'only add name to the usage log if the user has it locked
        Open ThisWorkbook.Path & "\usage.log" For Append As #file1
        Print #file1, Environ("USERNAME") & ". Please close all the additional workbooks that will be opened " _
        & "without saving them. Then contact the user that has it open or wait until they are finished."
        Close #file1
    Else
        'if someone else has the file open, find out who
        Open ThisWorkbook.Path & "\usage.log" For Input Access Read As #file1
            Do While Not EOF(file1)
               Line Input #file1, strLine
            Loop
        Close #file1
        MsgBox "The following user has the allocation sheets open: " & strLine
    End If
End Sub

These allocation sheet files act as a filing cabinet to store information. I have another file that is like a calculator to work out costs of jobs called my quoting tool. They are then copied across to the allocation sheet or filing cabinet for storage.

As I already mentioned, the above code works if the workbook is opened and someone else has it open. If enable macros has been selected by the user that has it open, the error message in the code will display but if they have not selected enable macros, the File not found error will be displayed which is not very meaningful. How can I change it so if that error occurs, I can display a message box instead of the file not found, asking the user to contact the person using "username" feature and get them to enable macros?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Sort of a chicken before the egg problem. If they don't enable macros, then, of course, you cannot use code. There may be some other native approach, but I don't know of it. You can set up your workbook in such a way that it is not functional unless macros are enabled.
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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