Consulting

Results 1 to 9 of 9

Thread: Solved: Run Code only if user types in a name

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Solved: Run Code only if user types in a name

    THis is the code that I wrote. However, it is cancelling weather the user inputs in the box or not. It should only pop up the message box if the user left the box blank or if they hit cancel. What am I missing?

    [VBA]On Error Resume Next
    myInput = InputBox("What do you want to name the Report?")
    If myInput Is Nothing Then
    MsgBox "You did not Create a Sales Report. Please rerun report or press Cancel."
    CallBlinco
    End

    Else
    End IF[/VBA]

  2. #2
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    Try this

    If IsNull(myInput) Then

    instead of

    If myInput Is Nothing Then

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Could you use a form with a textbox and have the ok button be disabled until they add text?
    [VBA]
    Private Sub Textbox1_Change()
    If Textbox1.Value <> "" Then
    CommandButton1.Enabled = True
    Else
    CommandButton1.Enabled = False
    End If
    End Sub
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    OBP,

    That is working Opposite. It is always running the full code.

  5. #5
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    Try this version then
    If (myInput) = "" Then


  6. #6
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    LUcas that worked great but there are two things that would make that the best.

    1) How do I clear the textbox of text after you press ok or cancel
    2) I want it so when you press enter after you type in the box it would automatically choose ok

    I tried this code that I wrote:

    [VBA]enter = TextBox1.EnterKeyBehavior = True
    If enter = True Then
    CommandOk_Click
    Else
    End If[/VBA]

  7. #7
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    OBP,

    That works perfectly. Thank you

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    to clear the textbox:
    [VBA]
    Private Sub CommandButton1_Click()
    Range("A1").Value = TextBox1.Value
    TextBox1.Value = ""
    End Sub
    [/VBA]
    It would probably help to add this initialize statement too:
    [VBA]
    Private Sub UserForm_Initialize()
    CommandButton1.Enabled = False
    End Sub
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Here is a code for using enter to add your data....adjust to suit your needs:
    [VBA]Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
    ByVal Shift As Integer)
    If KeyCode = 13 Then
    KeyCode = 0
    Sheet1.Range("A65536").End(xlUp).Offset(1, 0) = Me.TextBox1.Value
    Me.TextBox1 = vbNullString
    Me.TextBox1.SetFocus
    End If

    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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