Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Harry Hunter   on Nov 04 In MS Office Category.

  
Question Answered By: Jay Richards   on Nov 04

Some comments...
"Errors" and "trapping errors", usually refers to trapping system error  like
missing files or data base errors  and such like where the code *can't* do
what you want it to. What you are doing here rather, is testing for a set of
conditions.
I suspect you're coming from something like GWBasic. Use of GoTo nowadays is
normally not such a good idea.
Lots of If statements one after the other like that can be a bit
confusing... And that's what I suspect is happening here. Your test is
actually working but you may not be actually testing for what you want!
It's generally accepted better prctice to seperate If Then Else over
different lines rather than put the whole thing on one line.

You may want to step through your code using the debugger as well while
adding Matl, MatlCode and SubMatl as watches.

Rather than...
If Matl = "Coal" Then MatlCode = InputBox("1 = Lump 2 = ROM 3 = Fine-Graded
4 = Other Coal - Enter Code:", Title)

If Matl = "Coal" Then
MatlCode = InputBox("1 = Lump 2 = ROM 3 = Fine-Graded 4 = Other Coal
- Enter Code:", Title)
Endif

For the GoTo try...
Do
If....... then
....
ElseIf .....
....
Else
Exit do
EndIf
Loop

You have probably declared your variables somewhere... Can you post the
whole code maybe or if it's too big or whatever the variable declerations.
If you haven't declared the variables that's also good information for use.

It's normally good practice to be specific about what you are setting
variables to so...
Matl = Range("B12").text
Or
Matl = Range("B12").value
Is better than Matl = Range("B12"),

I think that the "And"s and "Or"s are confusing things.

Personaly unless I was *totally* positive about my code, I'd try and break
the If... code down to single choices so that I would be sure of what
This *And* That *Or* TheOther
was meant to be testing.

For...
If Matl = "Coal" And MatlCode < 1 Or MatlCode > 4 Then GoTo TryAgain
...What do you actually want?? Break it down...

' The following is just off the top of my head but maybe it'll give you a
start.
If ucase(Matl) = "ASPHALT" then
Select Case MatlCode
Case 1
SubMatl = "Asphalt Binder"
Case 2
SubMatl = "Asphalt, Crushed"
Case 3
InputBox.....
Case Else
' Do Nothing.
End Select
ElseIf Ucase(Matl) = "COAL" then
MatlCode = InputBox....
Select case MtlCode
Case 1, 2, 3
' Do something.
Case Else
' Do Nothing.
End Select
Else
' Not Coal or Asphalt.
EndIf

If you need any more then please do get back to us.

Share: 

 
 
Didn't find what you were looking for? Find more on Help - Error Trapping Doesn't Work Or get search suggestion and latest updates.


Tagged: