Problem with Error Handler routing?

G

Gerry O

I have a userform in Excel that requires a user to select an option from a
drop down combobox. A command button returns the value and opens up another
workbook for the user to edit. I added an error handling routine that
returns an error message if for some reason the file doesn't exist or there
is a problem with the file path. My problem is that even when the sub runs
successfully, I still get the error message. If I completely remove the
error handling routine and line code, I get no vba/excel errors, but the sub
runs successfully. I am not sure why I get the error message upon successful
completion of the sub?

Here is my code. Any help would be appreciated!

Private Sub cmbEnter_Click()
Dim CostCenter
CostCenter = cbxCostCenter.Value

On Error GoTo errorhandler

Workbooks.Open Filename:=ThisWorkbook.Path & "\" & "Budget 2008 - AMC ("
& CostCenter & ").xls", _
UpdateLinks:=1
Unload Me
'ResetAll
'ThisWorkbook.Close savechanges:=False

errorhandler:
MsgBox ("Application cannot load file. Contact Application
Administrator for assistance.")

End Sub
 
C

Chip Pearson

Gerry,

You need to add an "Exit Sub" line before your "errorhander" code label.
Without it, execution carries merrily along into the error handler code.
E.g.,


Exit Sub
ErrorHandler:
MsgBox("Error")
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
J

Jim Thomlinson

You need to exit sub otherwise your code will continue on into your
errorhandler...

Private Sub cmbEnter_Click()
Dim CostCenter
CostCenter = cbxCostCenter.Value

On Error GoTo errorhandler

Workbooks.Open Filename:=ThisWorkbook.Path & "\" & "Budget 2008 - AMC ("
& CostCenter & ").xls", _
UpdateLinks:=1
Unload Me
'ResetAll
'ThisWorkbook.Close savechanges:=False
Exit Sub '***************

errorhandler:
MsgBox ("Application cannot load file. Contact Application
Administrator for assistance.")

End Sub
 
G

Gerry O

Big "duh" on my part. Thanks for the quick response. Works perfectly now!
--
Thanks,

Gerry O.


Chip Pearson said:
Gerry,

You need to add an "Exit Sub" line before your "errorhander" code label.
Without it, execution carries merrily along into the error handler code.
E.g.,


Exit Sub
ErrorHandler:
MsgBox("Error")
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
G

Gerry O

Chip Pearson pointed out the same thing. Didn't eat my Wheaties this
morning. Thanks for the quick response!!
 

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