capturing the cancel option when using inputbox

pointers3e

New Member
Joined
Sep 2, 2007
Messages
10
hi

i am using the input box as follows:

dim strInput as string
strInput = Inputbox("enter your name", vbOKCancel)

i want to able to capture both pieces of information. how can i do this?

i think i need to use the application.inputbox version but i don't know how to use it so it does what i want...

thanks in advance!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
What 2 pieces of information?

As far as I know an inputbox only returns 1 piece of information.
 
Upvote 0
i think i need to use the application.inputbox version but i don't know how to use it so it does what i want...

Instead of using the Application.InputBox method when trapping Cancel, it is do-able with the function InputBox, as the example below demonstrates using strptr:


Code:
Sub InputBoxExample1() 
Dim ans1$ 
ans1 = InputBox("Please enter the name:", "Name") 
Select Case True 
Case StrPtr(ans1) = 0 
MsgBox "You hit Cancel.", 48, "Entry cancelled." 
Exit Sub 
Case Len(ans1) = 0 
MsgBox "You hit OK but entered nothing.", 48, "Entry scuttled." 
Exit Sub 
Case Else 
MsgBox "You entered ''" & ans1 & "''.", 64, "OK with entry" 
End Select 
End Sub


If you have your heart set on using the Application method, this is an example of that:

Code:
Sub InputBoxExample2()
Dim CancelTest As Variant
showInputBox:
CancelTest = Application.InputBox("Enter a value, or click Cancel to exit:")
If CancelTest = False Then
MsgBox "You clicked the Cancel button, Input Box will close.", 64, "Cancel was clicked."
Exit Sub
ElseIf CancelTest = "" Then
MsgBox "You must click Cancel to exit.", 48, "You clicked Ok but entered nothing."
GoTo showInputBox
Else
MsgBox "You entered " & CancelTest & ".", 64, "Please click OK to resume."
End If
End Sub
 
Upvote 0
Perhaps

Code:
Sub GetTextString()
Dim strInput As String
strInput = InputBox(prompt:="Please enter text")
If strInput = "" Then
    MsgBox "Null or Cancel"
Else
    MsgBox strInput
End If
End Sub
 
Upvote 0
Welcome to the Board!

InputBox doesn't support the button selection like a MsgBox does; OK/Cancel is the default. Note in your case that vbOKCancel results in a Title of "1".

Here's a pretty simple test:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> foo()
    <SPAN style="color:#00007F">Dim</SPAN> strInput <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
        strInput = InputBox("Enter your name", "What's yer name?", Application.UserName)
        MsgBox strInput
        <SPAN style="color:#00007F">If</SPAN> LenB(strInput) = 0 <SPAN style="color:#00007F">Then</SPAN> MsgBox "Canceled"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

You really don't need Application.InputBox unless you're limiting the Type property.

Hope that helps,

Smitty

EDIT: Vog beat me to the same idea. :LOL:
 
Upvote 0

Forum statistics

Threads
1,214,553
Messages
6,120,184
Members
448,949
Latest member
keycalinc

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top