Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 104
Default error handler not working correctly

Oh wise ones,
From the existing code below I crudely added some
error code from right out of the help section. The form would crap out to
debug mode if a valid red tag number was not entered. I just wanted it to
display a message and start the sub again. Now is displays a message but goes
to the next form and the data is put in the wrong cells. I would be shocked
if you guys took more the 3 seconds to fix this one. Please advise.

Thanks,
Mike

Sub Sup()
Dim Message, Title, Default, Squares
Dim closetag As Integer
On Error GoTo ErrorHandler<< I added this
Message = "Enter the red tag number that was reworked" ' Set
prompt.
Title = "Red Tag Number" ' Set title.
Default = "0" ' Set default.
closetag = InputBox(Message, Title, Default)
Worksheets("Closed Red Tags").Rows(4).Insert
Worksheets("Open Red Tags").Activate
With Worksheets("Open Red Tags").Range("a4:a500")
Set C = .Find(closetag, LookIn:=xlValues, LookAt:=xlWhole,
MatchByte:=True)
C.Select
Selection.Range("a1:k1").Cut
Worksheets("Closed Red Tags").Activate
Range("A4").Select
ActiveSheet.Paste
Worksheets("Closed Red Tags").Range("N4") = Date
Worksheets("Open Red Tags").Activate
Supervisor.Show
End With


Exit Sub << I added this
ErrorHandler: << I added this
MsgBox "Please enter a valid Redtag ID" << I added this
Resume Next << I added this

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default error handler not working correctly

Hey Mike,

If i understand you right, you would like to begin at the beginning of
the sub after the Error handler.
To do this you have to make a rule name (just like "ErrorHandler:") at
the beginnig of the sub.
secondly you have to make an adjustment to your messagebox so he know
what to do after the message.

If MsgBox("Please enter a valid Redtag ID", vbOKOnly) = vbOK Then GoTo
beginning

And i dont know if you have to use "Exit Sub" i never do, and it works
perfectly.

Greets Chris

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default error handler not working correctly


Well Mike,

It took me more than three seconds to figure out what your code wa
doing so does my answer qualify ?? :)

Firstly, error handlers need some sort of intelligence, so the way
write mine are like so:


Code
-------------------

Dim Found As Boolean
'*
On Local Error GoTo as_Err
...
...
...
as_Res:
Worksheets(Sheet_Name).Activate
On Local Error GoTo 0
Exit Sub
as_Err:
Select Case Err.Number
Case 9 '* Worksheet not found
Call Create_Sheet(Sheet_Name)
Resume as_Res
Case Else
On Local Error GoTo 0
Resume
End Select
End Sub

-------------------


So, the first thing to do is to use a -Local- error handler. If yo
don't do this then the error handler adds itself onto the chain o
previously declared error handlers and errors rattle up and down th
chain causing some difficult bugs in some cases.

The second thing to do is to turn your error handler off at the end o
the code otherwise any other errors elsewhere will use this erro
handler which is something you don't want.

Thirdly, give your error handler some intelligence. You don't want i
to react the same way for every error. Also give it a way to react t
an unexpected error. The 'Case Else' statement above is my 'give up
clause.

Every handler must be accompanied by a 'resume' statement. Resum
statements come in three flavours:


- Resume
- Resume Next
- Resume [Label]


Resume Resumes(!!) execution at the statement that caused the error.
Resume Next resumes at the statement after the error

Resume [label] (Such as the resume as_Res statement above) resumes at
particular statement.

What you need to do is to place a label (New_tag) like:


Code
-------------------

New_tag:
Message = "Enter the red tag number that was reworked" ' Set
prompt.
Title = "Red Tag Number" ' Set title.
Default = "0" ' Set default.
closetag = InputBox(Message, Title, Default)

-------------------


Code your error handler as I have shown above and then include th
statement


Code
-------------------

Resume New_tag

-------------------


instead of the

Code
-------------------


Resume Next


-------------------


Regards

Ric

--
Rich_
-----------------------------------------------------------------------
Rich_z's Profile: http://www.excelforum.com/member.php...fo&userid=2473
View this thread: http://www.excelforum.com/showthread.php?threadid=38481

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error handler linto Excel Discussion (Misc queries) 1 February 11th 10 12:17 PM
Error Handler sharad Excel Discussion (Misc queries) 1 September 17th 07 06:38 PM
Error Handler Not Working Bill Excel Discussion (Misc queries) 0 August 25th 05 07:13 PM
Error Handler dht[_2_] Excel Programming 5 August 19th 04 08:51 AM
Error Handler Not working 2nd time Todd Huttenstine Excel Programming 3 May 18th 04 09:37 PM


All times are GMT +1. The time now is 12:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"