Help On Error GoTo

Hi Everyone,

I`m creating a Workbook that protect and unprotect the sheets...

The part of protect and unprotect is ok!

But, the part of tratament of errors I don't get to do...

I want that when the workbook show the error, it go to Erro:


Answer this question

Help On Error GoTo

  • Turru

    Thiago

    If you whish to continue your program, you you must put your error-routine before 'Next'

    Your err-routine can be something like this

    Regards, FiftyFive

    Erro:

    Select Case Err
    Case 0
    MsgBox "Everything is OK"
    Case Else
    MsgBox "There is something wrong: " & Chr(10) & _
    Err & ": " & Err.Description
    End Select
    'next step in your routine
    'you can cleanup your err-trapper
    Err.Clear



  • p818632

    Guys,

    This is a strange one as Thiago's code worked fine when I tried it. Perhaps error handling was previously turned off prior to calling the method, has the OnError Goto Next statement has been called and does error handling need to be turned on using OnError Goto 0 (is it can't remember)

    Thiago create a new spreadsheet and try only unprotect process out on that.



  • sabfix

    Hi,

    This solution not resolved my problem...

    Sub desp_ws()
    On Error GoTo Erro
    Dim pa
    pa = ActiveSheet.Name

    Application.ScreenUpdating = False
    For Each ws In ActiveWorkbook.Sheets
    ws.Visible = True
    ws.Select
    ActiveSheet.Unprotect Password:=senha 'error here!
    Next
    Sheets(pa).Select
    Application.ScreenUpdating = True
    senha = Null
    Exit Sub

    Erro:

    Select Case Err
    Case 0
    MsgBox "Everything is OK"
    Case Else
    MsgBox "There is something wrong: " & Chr(10) & _
    Err & ": " & Err.Description
    End Select

    Err.Clear

    End Sub



  • GTG

    THiago

    I miss some declarations in your routine: 'ws' and 'senha'

    May be the vars are global Where do you set 'senha'

    In your example code is this step not nessesary: "Exit sub". The error-section works fine when

    An other example below

    I guess: Probably I don't understand your problem

    Regards Fiftyfive

    Sample code:

    Option Explicit

    Sub desp_ws()
    On Error GoTo Erro
    Dim pa, ws, senha
    pa = ActiveSheet.Name
    senha = "testje"
    Application.ScreenUpdating = False
    For Each ws In ActiveWorkbook.Sheets
    ws.Visible = True
    ws.Select
    ActiveSheet.Unprotect Password:=senha 'error here!
    Next
    Sheets(pa).Select
    Application.ScreenUpdating = True
    senha = Null

    Erro:

    Select Case Err
    Case 0
    MsgBox "Everything is OK"
    Case Else
    MsgBox "There is something wrong: " & Chr(10) & _
    Err & ": " & Err.Description
    End Select

    Err.Clear

    End Sub



  • Help On Error GoTo