Consulting

Results 1 to 14 of 14

Thread: Userform Textbox Number format

  1. #1
    VBAX Regular
    Joined
    Oct 2019
    Location
    South Africa; Mpumalanga
    Posts
    9
    Location

    Userform Textbox Number format

    Hi All.

    New to this forum.

    I have a userform where the user must fill in the odo meter reading of his trip. Start reading and end reading. This is then sent to the spreadsheet where these readings are used to calculate the trip distance.
    I always only used the spreadsheet and filled it in manually. I only got introduced to UserForms recently. This, for me is the next best thing since sliced bread. My formula used to work pretty well, but since I started to use the userform, the value from the TextBox is entered as text and not a number. This causes the formula not to calculate.

    Below is a extract of the code that I'm using.
    Range("P26").Value = Format(Me.cbESN, "# ##0")
    Please help me.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    With Range("P26")
      .Value = Val(cbESN)
      .NumberFormat= "# ##0"
    End With

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,745
    Location
    Slight tweak to Ken's

    I'd use

    If IsNumeric (cbESN) then .Value = Cdbl(cbESN)
    If you use Val, then it stops at the first non-digit. So "1,234" returns just 1 and "123a4" returns just 123

    You can never trust users to follow instructions
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Regular
    Joined
    Oct 2019
    Location
    South Africa; Mpumalanga
    Posts
    9
    Location
    Thanks.
    Will try and give feedback.

  5. #5
    VBAX Regular
    Joined
    Oct 2019
    Location
    South Africa; Mpumalanga
    Posts
    9
    Location
    Quote Originally Posted by Kenneth Hobs View Post
    With Range("P26")
      .Value = Val(cbESN)
      .NumberFormat= "# ##0"
    End With
    Thank you. It worked like a charm on the one spreadsheet and the UserForm that I have there. I now tried a similar format on a different spreadsheet and it is not working. I keep getting a error message: Unable to set the NumberFormat property for the Range class

    I actually wanted a different NumberFormat, but tried the exact same one that was working on the first sheet. I really don't know what I'm doing wrong.
        With Range("D28")
          .Value = Val(Me.tbNt)
          .NumberFormat = "# ##0"     ' Format  I actually want "0.0_ "  The format that I used on the other UserForm "# ##0_  " and it worked.
        End With

  6. #6
    VBAX Regular
    Joined
    Oct 2019
    Location
    South Africa; Mpumalanga
    Posts
    9
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Slight tweak to Ken's

    I'd use

    If IsNumeric (cbESN) then .Value = Cdbl(cbESN)
    
    Thanks for the reply. Can you please elaborate on where I must add this line of code?
    If you use Val, then it stops at the first non-digit. So "1,234" returns just 1 and "123a4" returns just 123

    You can never trust users to follow instructions

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,745
    Location
    1. This line

    With Range("P26")
      If IsNumeric (cbESN) then .Value = Cdbl(cbESN)
      .NumberFormat= "# ##0"End With

    2. What's in Me.tbNt?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    VBAX Regular
    Joined
    Oct 2019
    Location
    South Africa; Mpumalanga
    Posts
    9
    Location
    Quote Originally Posted by Paul_Hossler View Post
    1. This line

    With Range("P26")
      If IsNumeric (cbESN) then .Value = Cdbl(cbESN)
      .NumberFormat= "# ##0"End With

    2. What's in Me.tbNt?
    Me.tbNt is a textbox in a Timesheet. The number format for the hours worked is to be 0.00
    This is for another UserForm

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,745
    Location
    I meant what is the value in the Textbox?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Try doing it manually. The format string done manually should be what you use in code.

    If your sheet and cell was protected, the value setting should have caused an error.

    I don't know where your code resides so I don't know if it resides in the userform or not. Me is not really needed in userform code. It does not hurt anything though if it resides in the userform code.

  11. #11
    VBAX Regular
    Joined
    Oct 2019
    Location
    South Africa; Mpumalanga
    Posts
    9
    Location
    The value can be anything between 0 and 8 with 0.50 intervals. The code is in the UserForm in a CommandButton_click.

    I use the me for intelisence. Because I'm still new to vba, I need all the help from the program to assist me with the correct spelling and naming of code.

    I hope you understand what I mean.

  12. #12
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,745
    Location
    Again .. when you got the error, what was the SPECIFIC value in tbNt
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  13. #13
    VBAX Regular
    Joined
    Oct 2019
    Location
    South Africa; Mpumalanga
    Posts
    9
    Location
    Oh. Sorry, I didn't understand your question then. It was 1.

  14. #14
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Make a simple file and post it if you need further help.

    Don't forget to show a manually formatted cell to show the goal.

Posting Permissions

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