Другие языки программирования и технологии

VBA Excel Обработка ошибки

Как сделать так чтобы при ошибке с кодом 75, например, выводилось не системное сообщение а то сообщение, которое укажет пользователь?
Андрей Усов
Андрей Усов
6 725
из справки VBA:




On Error
Statement

Enables an error-handling routine and specifies the location of the routine
within a procedure; can also be used to disable an
error-handling routine.
Syntax
On Error GoTo line
On Error Resume Next
On Error GoTo 0
The On Error statement syntax can have any of the
following forms:




Statement
Description

On Error GoTo line
Enables the error-handling routine that starts at line specified in
the required line argument. The line
argument is any line label or line number. If a run-time
error
occurs, control branches to line, making the error handler
active. The specified line must be in the same procedure as the On Error statement; otherwise, a compile-time error occurs.

On Error Resume Next
Specifies that when a run-time error occurs, control goes to the statement immediately following the statement where
the error occurred where execution continues. Use this form rather than On Error GoTo when accessing objects.

On Error GoTo 0
Disables any enabled error handler in the current procedure.



и там же пример:
Sub InitializeMatrix(Var1, Var2, Var3, Var4)
On Error GoTo ErrorHandler
. .
Exit Sub
ErrorHandler:
. .
Resume Next
End Sub
илиSub OnErrorStatementDemo()
On Error GoTo ErrorHandler ' Enable error-handling routine.
Open "TESTFILE" For Output As #1 ' Open file for output.
Kill "TESTFILE" ' Attempt to delete open
' file.
On Error Goto 0 ' Turn off error trapping.
On Error Resume Next ' Defer error trapping.
ObjectRef = GetObject("MyWord.Basic") ' Try to start nonexistent
' object, then test for
'Check for likely Automation errors.
If Err.Number = 440 Or Err.Number = 432 Then
' Tell user what happened. Then clear the Err object.
Msg = "There was an error attempting to open the Automation object!"
MsgBox Msg, , "Deferred Error Test"
Err.Clear ' Clear Err object fields
End If
Exit Sub ' Exit to avoid handler.
ErrorHandler: ' Error-handling routine.
Select Case Err.Number ' Evaluate error number.
Case 55 ' "File already open" error.
Close #1 ' Close open file.
Case Else
' Handle other situations here...
End Select
Resume ' Resume execution at same line
' that caused the error.
End Sub
Абдрахманов Ильяс
Абдрахманов Ильяс
82 819
Лучший ответ
используй обработчик on error
Андрей Усов Я был бы признателен за более полный ответ.
Гугл и Яндекс к сожалекнию мне не сильно в этом плане помогли.