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!
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,216
Office Version
  1. 365
Platform
  1. Windows
What 2 pieces of information?

As far as I know an inputbox only returns 1 piece of information.
 

Tom Urtis

MrExcel MVP
Joined
Feb 10, 2002
Messages
11,224
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
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
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:
 

Watch MrExcel Video

Forum statistics

Threads
1,122,216
Messages
5,594,886
Members
413,947
Latest member
gizmolucy

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
Top