error problem

  • Thread starter Pierre via OfficeKB.com
  • Start date
P

Pierre via OfficeKB.com

Hi,

i have an application with lots of sheets, forms and modules and subs.
with each error i would like the following code to be executed:

private sub error()
With Sheets("waarschuwing")
Dim c As Integer
.Visible = xlSheetVisible
For c = 1 To Sheets.Count
If Sheets(c).Name <> "waarschuwing" Then
Sheets(c).Visible = xlSheetVeryHidden
End If
Next
End With
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub

however, the sub is in a module.
I would like to have the on erro goto statement go to this sub in the error
handling module.
How can i do that in VBA?
Thanks,
Pierre
 
G

Guest

First, you would need to make your sub Public, not Private, to be visible
across modules. Also, I would not name it Error() as that is a reserved word
in VBA causing potential confusion with the VBA built-in Error function. So
I will call it "MyError" here.

Then, in each function/sub where you wanted to use this as your error
handler, you could do as illustrated below:

Sub MySub()
On Error goto MyErrHandler

' Regular code for sub goes here

Exit Sub

MyErrHandler:
MyError
End Sub

Since your code closes the active workbook, I am assuming you do not need
any code to continue at that point, but if you do need to continue, put a
Resume Next statement after you call MyError:

MyErrHandler
MyError
Resume Next
End Sub
 
P

Pierre via OfficeKB.com

Thanks K.
This helps a lot !
Pierre

K said:
First, you would need to make your sub Public, not Private, to be visible
across modules. Also, I would not name it Error() as that is a reserved word
in VBA causing potential confusion with the VBA built-in Error function. So
I will call it "MyError" here.

Then, in each function/sub where you wanted to use this as your error
handler, you could do as illustrated below:

Sub MySub()
On Error goto MyErrHandler

' Regular code for sub goes here

Exit Sub

MyErrHandler:
MyError
End Sub

Since your code closes the active workbook, I am assuming you do not need
any code to continue at that point, but if you do need to continue, put a
Resume Next statement after you call MyError:

MyErrHandler
MyError
Resume Next
End Sub
[quoted text clipped - 21 lines]
Thanks,
Pierre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top