Consulting

Results 1 to 7 of 7

Thread: Solved: space bar

  1. #1

    Solved: space bar

    How can I prevent the first character that is type from being a space (created by the space bar)?

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Maybe:[VBA]Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count = 1 Then
    Target.Value = LTrim(Target.Value)
    End If
    End Sub
    [/VBA]

  3. #3
    Hi GTO,

    Sorry, but I forgot to mention it should be applied in a userform textbox? How can i apply this to a textbox?

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Did you try the exit event?

  5. #5
    not sure what you mean...apply the same code in the exit event? I'll get an error.

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by av8tordude
    not sure what you mean...apply the same code in the exit event? I'll get an error.
    I meant the exit event of the textbox. In a new/blank workbook, create a userform with three textboxes.
    [vba]Option Explicit

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim MeCtl As MSForms.TextBox
    Set MeCtl = Me.ActiveControl
    TrimLeft MeCtl
    Me.Caption = ">" & MeCtl.Value & "<"
    End Sub

    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim MeCtl As MSForms.TextBox
    Set MeCtl = Me.ActiveControl
    TrimLeft MeCtl
    Me.Caption = ">" & MeCtl.Value & "<"
    End Sub

    Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim MeCtl As MSForms.TextBox
    Set MeCtl = Me.ActiveControl
    TrimLeft MeCtl
    Me.Caption = ">" & MeCtl.Value & "<"
    End Sub

    Private Function TrimLeft(ByRef tb As MSForms.TextBox)
    tb.Value = LTrim(tb.Value)
    End Function[/vba]
    You can watch the userform's caption to see that the values entered into whichever textbox are trimmed on the left (leading spaces) upon exiting the textbox.

    What exactly are you wanting to do? I ask as I would suspect that trimming in the textbox is not the issue. Wouldn't we be more likely be concerned with what is happening with the textbox's value as it affects other parts of the code (ie - the val stored in the variable). I hope that makes sense.

    Mark

  7. #7
    Thank you Mark. your code helps a lot

Posting Permissions

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