Consulting

Results 1 to 8 of 8

Thread: Need input box when user does not enter a number or presses cancel

  1. #1

    Need input box when user does not enter a number or presses cancel

    Need to exit sub if user clicks cancel.

    If I change the variables to strings it will work. However I need the variables to be doubles.



    'Declare Variables
    Dim NumberOfLocations As Double
    Dim isValid As Boolean
    Dim FirstLocation As Double
    Dim SecondLocation As Double








    Sub CalculateLineShare()
    ' Making sure the boolean variable is set to false
    isValid = False
    'start of loop to make sure user puts in an integer
    Do Until isValid = True
    NumberOfLocations = InputBox("Enter Number of Locations", "Enter Number")
    ' right here is where the problem is at if the variables are strings and the user clicks cancel every thing works.
    If NumberOfLocations = "" Then
    Exit Sub
    End If

    If IsNumeric(NumberOfLocations) Then
    isValid = True
    Else
    MsgBox "You must enter an Integer to continue"
    End If


    Loop
    If NumberOfLocations <= 2 Then
    FirstLocation = InputBox("Enter Number of Pieces used at first location", "Enter Number")
    SecondLocation = InputBox("Enter Number of Pieces used at Second location", "Enter Number")
    Else
    End If


    End Sub

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Dim uiString As String
    
    uiString = InputBox("Enter something")
    
    If StrPtr(uiString) = 0 Then
        MsgBox "cancel pressed"
        Exit Sub
    Else
        If uiString = vbNullString Then
            MsgBox "user entered null string"
        Else
            MsgBox "user entered " & uiString
        End If
    End If

  3. #3
    The variable can not be a string. It has to be a double. I can get it to work if the variable is a string.

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    If you need to convert a string to a double, then use cdbl().

    Inputbox return value must be a string.

    InputBox Function

     This page is specific to the Visual Basic for Applications (VBA) Language Reference for Office 2010. Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a String containing the contents of the text box.

  5. #5
    I was just looking up how to convert a string to a single or a double. A single would also work. Thanks for your help.

    Can I ask you one more question?

    I am trying to get an if statement to work in VBA and having trouble with some thing that should be simple.


    Here is the Pseudocode.

    If x >1 and <= 2 then
    Msgbox Here is your output
    else exit sub.

    The first line is not working for some reason. When I try to type the second condition <= 2 I get expected expression.
    I know that this should be simple for some one who uses vba on a regular basis. I just use it once in a while. thank you so much for your help.

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    An alternative would be to use Application.InputBox rather InputBox

    Dim uiValue As Double
    
    uiValue = Application.InputBox("Enter", Type:=1)
    If uiValue = 0 Then MsgBox "canceled": Exit Sub
    
    MsgBox "User entered " & uiValue
    One problem with that approach is that it can't distinguish between the user entering 0 and the user pressing cancel.


    Other question:

    Try
    If 1 < x And x <= 2 Then

  7. #7
    Thank you both for you help.

    I forgot that you need to restate the variable in an if statement.

    I work in a factory and the people I work with do not realize that you can not remember all this syntax. They will not give us internet access they just think that you should be able to remember all this syntax. Which if I did this every day I would have a chance but some times I can go 6 months before we need to write a vba program.

  8. #8
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    A lot of syntax can be found with Excel, no external link needed. The Macro Recorder and the Object Browser are two onboard resources that I use often.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •