Error handling and give message

Linda Lo

New Member
Joined
May 15, 2023
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
Dear all,

I want to have a massage instead of Debug message to the user when running time error 9 . When running error, message was shown instead of Debug box. In the other side, message was also shown in normal run. What is missing in my Marco? Please help me to point out? Much Thanks.

Linda Lo



Sub myMacro()

Dim str As String

On Error GoTo myError

Sheets("str").Activate

myError:
MsgBox "Date of record was incorrect for the file, please replace it!"


End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You must have an Exit Sub statement so the macro does not display the error message when there is no error.

VBA Code:
Sub myMacro()
  
    Dim str As String

    On Error GoTo myError
    Sheets("str").Activate
    On Error GoTo 0
  
    Exit Sub

myError:
    If Err.Number = 9 Then
        MsgBox "Date of record was incorrect for the file, please replace it!"
    Else
        MsgBox "Run-time Error '" & Err.Number & "':" & vbCrLf & Err.Description, vbOKOnly Or vbExclamation, "Error"
    End If
End Sub

(Tip: For future posts , please try to use code tags like I did above when posting code. It makes your code easier to read and copy.
)
 
Upvote 1
Solution

Forum statistics

Threads
1,215,109
Messages
6,123,137
Members
449,098
Latest member
Doanvanhieu

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