+ Reply to Thread
Results 1 to 11 of 11

Thread: Formatting textbox

  1. #1
    Registered User
    Join Date
    10-23-2008
    Location
    Long Beach, WA, USA
    Posts
    20

    Question Formatting textbox

    This should be easy but escapes me. How do I format a textbox on a user form so that the text will read as a fraction. I use a slider/ textbox combo. Then the code divides the slider amount by 32. I want the result to read as 7/32" etc.

    Thanks again for being so on top of things and willing to help us neophites out here!
    Last edited by blue9244; 12-18-2008 at 08:36 PM.

  2. #2
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777
    textbox1.value = format(somenumber, "# ??/??")
    If you want to round it to the nearest 1/32, then
    textbox1.value = format(round(somenumber * 32, 0) / 32, "# ??/??")
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Cochrane,Alberta
    MS-Off Ver
    XL 2003,2007,2010
    Posts
    6,843
    This code should work for you
    Private Sub TextBox1_Change()
        If IsNumeric(TextBox1.Value) Then
            Label1.Caption _
                  = Application.Text(Round(TextBox1.Value / 0.03125, 0) * 0.03125, "# ??/??")
        Else
            Label1.Caption = "not a number in textbox1"
        End If
    End Sub
    Check out the sample workbook
    http://www.freewebs.com/davesexcel/f...20userform.xls

  4. #4
    Registered User
    Join Date
    10-23-2008
    Location
    Long Beach, WA, USA
    Posts
    20
    Thanks so very much! That ought to do it!

  5. #5
    Registered User
    Join Date
    10-23-2008
    Location
    Long Beach, WA, USA
    Posts
    20
    I think I marked this one as solved a little too soon. All I'm getting in the text box is: ??/?? and no value... If I leave the code just as you depicted it, I get an interger with ??/?? as a suffix. Here is what I have:

    Private Sub FingerOpen1_Change()
        Dim FingerOpenVal1 As Variant
        FingerOpenVal1 = Val(FingerOpen1.Text)
        If FingerOpenVal1 >= ScrollBar16.Min And FingerOpenVal1 <= ScrollBar16.Max Then
            ScrollBar16.Value = FingerOpenVal1
        End If
    End Sub
    Private Sub ScrollBar16_Change()
        FingerOpen1.Value = Format(Round(ScrollBar16.Value * 32, 0) / 32, "# ??/??")
    End Sub
    Thanks again. I'm sure I'm just missing something very obvious to you guys!

    BTW, why would you multiply by 32 only to divide by 32 - doesn't that leave you where you started?

  6. #6
    Registered User
    Join Date
    10-23-2008
    Location
    Long Beach, WA, USA
    Posts
    20
    Could someone please tell me where my error is in the above code? I've tried everything I can think of and nothing works. I'm sure your examples were fine - I just must have missed something along the way.

    Please and thanks very much!

  7. #7
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777
    We don't have your form or workbook, so debugging is up to you. When you step through the ScrollBar16_Change() code, what's the value of ScrollBar16?

    why would you multiply by 32 only to divide by 32 - doesn't that leave you where you started?
    If you mutiply by N, round to a whole number, and divide by N, the result is rounded to the nearest multiple of 1/N.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Registered User
    Join Date
    10-23-2008
    Location
    Long Beach, WA, USA
    Posts
    20

    Smile

    I didn't answer quickly because I was attempting to think this through myself but I'm still at a loss. The value of the ScrollBar changes as the user slides the scroll bar. At one point I had it at 108 and stepped through the procedure dividing by 32, I obtained the value I wanted - i.e. 3.375. The text box returns the value as 3.375 correctly. I want it to read 3 3/8 in this case. When I apply the formatting, I get either a type mis-match or the result in the text box as 108 ??/?? depending on where I've tried to apply the formatting.

    Here's the code to obtain 3.375 (or any other number selected by the scroll bar).

    Private Sub FingerOpen1_Change()
        Dim FingerOpenVal1 As Variant
        FingerOpenVal1 = Val(FingerOpen1.Value)
        If FingerOpenVal1 >= ScrollBar16.Min And FingerOpenVal1 <= ScrollBar16.Max Then
            ScrollBar16.Value = FingerOpenVal1
        End If
    End Sub
    Private Sub ScrollBar16_Change()
        FingerOpen1.Value = ScrollBar16.Value / 32
    End Sub
    Where FingerOpen1 is Textbox1. Does this help at all?

  9. #9
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777
    Huh; who knew Format wouldn't do fractions?

    Change Format to WorksheetFunction.Text
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359
    Hi

    I've got a funny feeling that FORMAT doesn't like the fraction component.

    try something like

    FingerOpen1.Value = evaluate("=text(" & ScrollBar16.Value / 32 & ",""# ??/32"")")
    EDIT: Darn - shg you beat me to it...

    rylo

  11. #11
    Registered User
    Join Date
    10-23-2008
    Location
    Long Beach, WA, USA
    Posts
    20
    That did it!!!! Thanks so very much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0