Consulting

Results 1 to 10 of 10

Thread: Addin for work

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

    Addin for work

    I am taking XLGibbs advice and putting all my questions into this one post. Not all at once though. First, I want to start with 2 pretty simple things I am racking my brain with.

    1) How do I get a textbox in a userform to be selected when the form is initialized, so you can just start typing?

    2) How do I get it so when you are done typing in that same textbox, you just hit enter and the rest of my code will run?

  2. #2
    VBAX Mentor Brandtrock's Avatar
    Joined
    Jun 2004
    Location
    Titonka, IA
    Posts
    399
    Location
    Attached is a small example doing both of the things you asked.

    Didn't know how you wanted the userform to activate. Just run it from Tools>Macro>Macros and select the RunUserform macro.

    HTH,
    Brandtrock




  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Djblois
    1) How do I get a textbox in a userform to be selected when the form is initialized, so you can just start typing?
    Just set the control's SetFocus property

    [vba]

    Me.TextBox1.SetFocus
    [/vba]

    If you want the text all pre-selected, you can also do that

    [vba]


    With Me.TextBox1
    .SelStart = 0
    .SelLength = Len(.Text)
    .SetFocus
    End With
    [/vba]

    Quote Originally Posted by Djblois
    2) How do I get it so when you are done typing in that same textbox, you just hit enter and the rest of my code will run?
    Use the Exit event of the textbox.

    [vba]

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    MsgBox "Exiting"
    End Sub
    [/vba]

    This will also trigger on any exit action, tab or selecting another control, but it does give you a way to execute some code when exiting, and cancel that exit if necessary.

  4. #4
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Thank you both for you help. XLD, the set focus works perfectly but the second part won't work for me because the textbox starts with the focus. If someone wants to change the settings now they would not be able to.

    Thank you,
    Daniel

  5. #5
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Is there another way to get the enter key to work???

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by Djblois
    Is there another way to get the enter key to work???
    What do you mean?

  7. #7
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    I think what he's looking for is when you press Enter, it will effectively "click" the OK button to finalize the choices.

    To do that, change the DEFAULT property on your OK button to TRUE.
    Office 2010, Windows 7
    goal: to learn the most efficient way

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    [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

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

    Thank you

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

    I get this error sometimes, "Runtime error '1004': Unable to get the LabelRange Property of the Pivotfield class" Does anybody know what this means so I can debug it?

Posting Permissions

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