• vba coding (97)

    Author
    Topic
    #360866

    Hi All

    Please help me with the vba coding: I have an input box where the user must input a figure. How do I ignore the rest of the coding behind the command button when the user press cancel instead of enter the figure?

    Cheers and thanks for your help.

    Viewing 2 reply threads
    Author
    Replies
    • #544371

      strAnswer = InputBox(“Please enter a figure”, “Enter a Figure”)
      If strAnswer = ” ” _
      Then
      Exit Sub
      End If

      …or sumthin’ like that…

      The inputbox returns an empty string if the user presses cancel.

      You probably want use IsNumeric to check if the answer is a number. I’m assuming when you say “figure” you mean a number.

    • #544373
    • #544532

      In Excel, you also can use the InputBox method. It has one additional parameter, Type, so that you can force your users to enter a particular type of data: a number, text, or even a range. Checking for Cancel is a little tricky, Excel returns False, but VBA will convert lots or stuff into a False. Here is how I would get only a numerical response from the user:

      Option Explicit
      Sub Macro1()
      Dim rsp As Variant
          rsp = Application.InputBox("Enter a number", , 10, , , , , 1)
          If TypeName(rsp) = "Boolean" Then Exit Sub
          MsgBox "You're #" & rsp
      End Sub
      

      Notice that I never bothered to check for False: the only time Excel will return a Boolean is when the Cancel button is pressed. Be sure to include the Application before InputBox; otherwise you’ll get a syntax error.

      Excel’s InputBox method is most useful to get the user to select a Range. Hope this helps! –Sam

    Viewing 2 reply threads
    Reply To: vba coding (97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: