View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default best approach for overcoming Input box errors & code optimisation?

If you have to ask if using On Error Resume Next is okay to do, I'd have to
say no, it is not okay... you should only use that statement when you
understand the ramifications of doing so; that is, you know the types of
errors that can be generated and you know that your code can survive those
errors if they are ignored. Now, as to your other question... there was too
many postings to go through to in order to figure out where the code you
were talking about is (it is usually best to post any code directly in your
message so the volunteers here don't have to go searching for it).

I'm not sure what you want to do if the user clicks Cancel (end the
subroutine, substitute in a default value for the rest of the code to use,
or something else), so let me give you a general structure that you can use
after an InputBox statement in order to determine whether the user pressed
Cancel or not.... then you can replace my MsgBox statements with the code
you want to execute for the various possibilities. Copy/Paste the following
macro code into a code window and run it... try clicking Cancel, Enter with
no text and then Enter with some text in order to see how you can trap each
of these possible actions.

Sub Test()
Dim strInput As String
strInput = InputBox("Some prompt for input")
If Len(strInput) = 0 Then
If StrPtr(strInput) = 0 Then
MsgBox "User clicked Cancel Button"
Else
MsgBox "No text entry, user clicked Enter"
End If
Else
MsgBox "The user inputted some text"
End If
' The subroutine code continues here
End Sub

Rick


"broro183" wrote in message
...
hi all,

StephenR & myself have been helping a user modify a macro which will
coordinate price changes on inventory based on user inputs from Input
boxes
at http://Excelforum.com. If the Input box is cancelled a mismatch error
occurs due (I think) to the previous variable declaration as long.
My reading suggests that it is best to declare variables explicitly rather
than use variants so to apply this I have used "on error resume" to make
the
code work.

Is this the best approach?

For more background please refer to the below thread & can you please
reply
to the below thread for the original op?
http://excelforum.com/showthread.php?t=621111

Also, feel free to make any suggestions for improving the current code...

I apologise if this is not the best way for asking for help. If it's not,
can someone please let me know how I should ask?

Thanks
Rob

__________________
Rob Brockett
NZ
Always learning & the best way to learn is to experience...