ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   If statement question (https://www.excelbanter.com/excel-programming/422558-if-statement-question.html)

Eric S[_2_]

If statement question
 
I have a macro that pops up a message box asking the user for an
number input. I want to have the macro run for all inputs greater
than 1. If the user puts in "1", I want to have the macro just ignore
it and do nothing. Is that possible with an if statement? Is that
the best way?

If the user doesn't input "1", I want the macro to do the normal
operations it was coded to do.

Any thoughts? Some code as an example would help greatly since I'm
new with VBA.

Thanks!

Mike H

If statement question
 
Hi,

You could do this

Sub surface()
Response = InputBox("Input a number 1", "Input")
If Response = 1 Then Exit Sub
'do things
End Sub

Mike

"Eric S" wrote:

I have a macro that pops up a message box asking the user for an
number input. I want to have the macro run for all inputs greater
than 1. If the user puts in "1", I want to have the macro just ignore
it and do nothing. Is that possible with an if statement? Is that
the best way?

If the user doesn't input "1", I want the macro to do the normal
operations it was coded to do.

Any thoughts? Some code as an example would help greatly since I'm
new with VBA.

Thanks!


RadarEye

If statement question
 
On 16 jan, 16:37, Eric S wrote:
I have a macro that pops up a message box asking the user for an
number input. *I want to have the macro run for all inputs greater
than 1. *If the user puts in "1", I want to have the macro just ignore
it and do nothing. *Is that possible with an if statement? *Is that
the best way?

If the user doesn't input "1", I want the macro to do the normal
operations it was coded to do.

Any thoughts? *Some code as an example would help greatly since I'm
new with VBA.

Thanks!


Hi Eric,

In Excel2003 I have created the code below.
Since an InputBox returns a string some typecasting might be
necessary.
When the user click Cancel an empty string is returned.

Sub EricS()
Dim strReply As String
Dim strMessage As String

Do
strReply = InputBox(strMessage, "Enter a number")

If IsNumeric(strReply) Then
If Val(strReply) = 1 Then
Exit Do
Else
' DO YOUR THING
End If
Else
If Len(strReply) = 0 Then
Exit Do
Else
strMessage = "That was not a number, try again"
End If
End If
Loop
End Sub

HTH,

Wouter

Eric S[_2_]

If statement question
 
Hi Mike -

Thanks for your help - worked perfectly!

Probably seemed like an overly simple issue to you, but I learned
something new, so THANKS.

Eric


All times are GMT +1. The time now is 07:25 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com