Error handler not executing VBS past "Resume Next" statement?

NessPJ

Active Member
Joined
May 10, 2011
Messages
414
Office Version
  1. 365
Hi all,

I made an error handler, but i would like to execute a few steps after reporting the error. But the routine does not seem to be executing them.

Here's my code:
VBA Code:
Private Sub Test()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

On Error GoTo Error

DivisionByZero = 1 / 0

'Error Handler

Error:

If Err.Number <> 0 Then

    Msg = "Error # " & Str(Err.Number) & " was generated by " _
    & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
   
    MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
   
End If

Resume Next

PROTOFF (Password)

Sheets("Parameters").Activate
Sheets("Parameters").Range("B9").Value = "#ERR"

Sheets("Menu").Activate

PROTON (Password)

Application.ScreenUpdating = True
Application.DisplayAlerts = True

Exit Sub

End Sub

Private Function PROTOFF(Password As String)
    
' Loop through all sheets in the workbook

    For i = 1 To Sheets.Count
 
    Sheets(i).Unprotect (Password)

    Next i

End Function

Private Function PROTON(Password As String)
  
    For i = 1 To Sheets.Count
    
    Sheets(i).Protect DrawingObjects:=True, Contents:=True, AllowUsingPivotTables:=True, Scenarios:=True _
    , AllowFiltering:=True, Password:=Password

    Next i

End Function

Am i doing something wrong?
 

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).
Hi,
probably where you have sited your error reporting & resume next statement in your code.

try this update & see if does what you want


VBA Code:
Private Sub Test()
  
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
  
    On Error GoTo Error
  
    DivisionByZero = 1 / 0
  
    PROTOFF (Password)
  
    Sheets("Parameters").Activate
    Sheets("Parameters").Range("B9").Value = "#ERR"
  
    Sheets("Menu").Activate
  
    PROTON (Password)
  
'Error Handler
Error:
    If Err.Number <> 0 Then
      
        Msg = "Error # " & Str(Err.Number) & " was generated by " _
        & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
      
        MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
 
        Err.Clear
      
        Resume Next
      
    End If
  
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
  
End Sub

Dave
 
Upvote 0
Hello Dave,

Thanks for your reply! In your example the value #ERR would always be written, also during normal execution of the routine? :)
The idea was for this value to be written when an actual error was triggered.
 
Upvote 0
Error reporting should only be reported when an error occurs otherwise, that section of code should not execute.

Dave
 
Upvote 0
Resume Next sends you back to the line after the one that caused the error.
 
Upvote 0

Forum statistics

Threads
1,213,521
Messages
6,114,109
Members
448,548
Latest member
harryls

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