Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
InputBox buttons
Hi,
How do I capture the OK or Cancel button on an input box? I've tried this but I get a type mismatch error... Dim NewHeader as String If NewHeader = vbOK Then 'if input box was not cancelled -- Thanks for your help. Karen53 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
InputBox buttons
Karen,
If I remember correctly, you can't capture the buttons from the input box. The input box returns a string. If you click OK, then it retruns whatever is in the textbox. If you click Cancel, it returns an empty string "" (...or does it return FALSE...look up input box in HELP). Then you would test the button like this: If NewHeader = "" then 'message to user that "Cancel" was pushed exit sub else 'commands to work with string entered in the input box end if HTH, Conan "Karen53" wrote in message ... Hi, How do I capture the OK or Cancel button on an input box? I've tried this but I get a type mismatch error... Dim NewHeader as String If NewHeader = vbOK Then 'if input box was not cancelled -- Thanks for your help. Karen53 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
InputBox buttons
The constants returned by a message box are numeric values, not strings. Try
something similar to the code below to capture the click to a variable named intOK dim intOK as Integer intOK = MsgBox("Yada, yada, yada...",vbQuestion+vbOkCancel,"Title Goes Here") More code here based upon response... Go to the VBE (Alt + F11) and in the Help Question Box type "Intrinsic Constants" sans quotes. Then select MsgBox constants from the result list to see a full list of message box buttons and their return values. -- Kevin Backmann "Karen53" wrote: Hi, How do I capture the OK or Cancel button on an input box? I've tried this but I get a type mismatch error... Dim NewHeader as String If NewHeader = vbOK Then 'if input box was not cancelled -- Thanks for your help. Karen53 |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
InputBox buttons
Using vbOK, vbCancel, vbAbort etc. works for the MsgBox function. These are
enumerated (builtin) integer constants (technically type Long) recognized by VBA. For example: MsgBox vbOK MsgBox vbCancel MsgBox vbAbort MsgBox vbRetry MsgBox vbIgnore InputBox returns a string containing the content of the what was typed in the textbox or an empty string ("") if the Cancel button was clicked. Same happens if you don't type anything and click OK, which amounts to the same thing so this should't be a problem. Try: Dim NewHeader as String NewHeader = InputBox("Specify a header name...") If NewHeader < "" Then ActiveCell.Value = NewHeader Greg "Karen53" wrote: Hi, How do I capture the OK or Cancel button on an input box? I've tried this but I get a type mismatch error... Dim NewHeader as String If NewHeader = vbOK Then 'if input box was not cancelled -- Thanks for your help. Karen53 |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
InputBox buttons
Hi Karen,
I am not sure if you can do this with the standard InputBox. You can test the return value, but not which button is pressed, as far as I can see. if you use Application.Inputbox, which is slightly different, then you can try something like: If Application.InputBox("Blah blah") = False Then However, if you are asking for text to be returned and the user inputs "False" then your code will act in the same way as when you are trying to capture the Cancel button. The Application.InputBox does allow you to specify the type of data being returned, and, therefore, if it is not text that you want to be returned (say you specify that it must be a Range or a number that is to be returned) then you can be sure that the only time False will come back is if Cancel is pressed. If you do specify something other than text and wish to use this method then you will find that an error occurs when cancel is pressed (as False is not a number or Range etc) so you will have to use On Error Resume Next before the Inputbox line. This is all I can suggest of the top of my head, without you having to design your own form to do this. Hope this is of some help, Sean. -- (please remember to click yes if replies you receive are helpful to you) "Karen53" wrote: Hi, How do I capture the OK or Cancel button on an input box? I've tried this but I get a type mismatch error... Dim NewHeader as String If NewHeader = vbOK Then 'if input box was not cancelled -- Thanks for your help. Karen53 |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
InputBox buttons
hi
i think you're using it slight the wrong way. Dim st As String st = InputBox("enter something") If Not st = "" Then 'user clicked cancel 'do something else do something else end if regards FSt1 "Karen53" wrote: Hi, How do I capture the OK or Cancel button on an input box? I've tried this but I get a type mismatch error... Dim NewHeader as String If NewHeader = vbOK Then 'if input box was not cancelled -- Thanks for your help. Karen53 |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
InputBox buttons
hi
try this Dim r As String r = InputBox("enter something") If Not r = "" Then 'user clicked cancel 'do something else 'do something else end if Regards FSt1 "Karen53" wrote: Hi, How do I capture the OK or Cancel button on an input box? I've tried this but I get a type mismatch error... Dim NewHeader as String If NewHeader = vbOK Then 'if input box was not cancelled -- Thanks for your help. Karen53 |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
InputBox buttons
sorry for the double post. i got caught up in a email varification loop and
didn't know my first post went through. Has anyone noticed that communities newsgroups has been acting a little weird lately? regards FSt1 "FSt1" wrote: hi try this Dim r As String r = InputBox("enter something") If Not r = "" Then 'user clicked cancel 'do something else 'do something else end if Regards FSt1 "Karen53" wrote: Hi, How do I capture the OK or Cancel button on an input box? I've tried this but I get a type mismatch error... Dim NewHeader as String If NewHeader = vbOK Then 'if input box was not cancelled -- Thanks for your help. Karen53 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Option Buttons/Radio Buttons | New Users to Excel | |||
have toggle buttons but everytime print preview buttons move | Excel Discussion (Misc queries) | |||
Replacing macro buttons with a new set of buttons | Excel Programming | |||
buttons on InputBox | Excel Programming | |||
Inputbox and Application.InputBox | Excel Programming |