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

NessPJ

Active Member
Joined
May 10, 2011
Messages
395
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?
 

Some videos you may like

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,479
Office Version
  1. 2019
Platform
  1. Windows
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
 

NessPJ

Active Member
Joined
May 10, 2011
Messages
395
Office Version
  1. 365
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.
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,479
Office Version
  1. 2019
Platform
  1. Windows
Error reporting should only be reported when an error occurs otherwise, that section of code should not execute.

Dave
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,453
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Resume Next sends you back to the line after the one that caused the error.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,328
Messages
5,624,036
Members
416,007
Latest member
csf

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
Top