Consulting

Results 1 to 7 of 7

Thread: How to format a TextBox as currency

  1. #1

    How to format a TextBox as currency

    Here is my code....

    Private Sub CommandButton1_Click()
    With ActiveDocument
       .SelectContentControlsByTitle("Date").Item(1).Range.Text = Me.TextBox1
       .SelectContentControlsByTitle("FirstName").Item(1).Range.Text = Me.TextBox2
       .SelectContentControlsByTitle("LastName").Item(1).Range.Text = Me.TextBox3
       .SelectContentControlsByTitle("LastName2").Item(1).Range.Text = Me.TextBox3
       .SelectContentControlsByTitle("CreditLimit").Item(1).Range.Text = Me.TextBox4
       .SelectContentControlsByTitle("Entity").Item(1).Range.Text = Me.TextBox5
       .SelectContentControlsByTitle("CreditScore").Item(1).Range.Text = Me.TextBox6
       .SelectContentControlsByTitle("Creditdate").Item(1).Range.Text = Me.TextBox6
       .SelectContentControlsByTitle("CreditLow").Item(1).Range.Text = Me.TextBox7
       .SelectContentControlsByTitle("CreditHigh").Item(1).Range.Text = Me.TextBox8
       .SelectContentControlsByTitle("Address").Item(1).Range.Text = Me.TextBox9
       .SelectContentControlsByTitle("City").Item(1).Range.Text = Me.TextBox10
       .SelectContentControlsByTitle("Creditdate").Item(1).Range.Text = Me.TextBox11
       .SelectContentControlsByTitle("RelationshipManager").Item(1).Range.Text = Me.TextBox12
    End With
    End Sub
    This code is within a userform CommandButton

    There is a template I have set up and used RichText as placeholders to where the information will be sent. I need the Rich Textbox "CreditLimit" or the the TextBox4 to equal a currency value in the format of $1,000,000. Please tell me if this is possible and
    Last edited by Aussiebear; 04-12-2023 at 06:17 PM. Reason: Adjusted the code tags

  2. #2
    This is how I do it on one of my textboxes on a form (SalesPrice is my form textbox):

    SalesPrice.Value = Format(SalesPrice.Value, "$###,##")

    Then when they click Submit:

    Private Sub SubmitButton_Click()
    PurchasePrice = SalesPrice.Value
    End Sub

    I make PurchasePrice a Public Global variable so I can carry it over to my macro or macros from the form. So I mean this should work for what you need:

    PurchasePrice = Format(PurchasePrice, "$###,##")
    Last edited by Aussiebear; 04-12-2023 at 06:18 PM. Reason: Added code tags

  3. #3
    Thank you! Yeah that worked perfectly. I know this is probably beyond what VBA can do but is there anyone to have it pre populated with a $ sign and then also as the user is typing it automatically formats the text in the Userform to display what is is going to be displayed on the document?

  4. #4
    Quote Originally Posted by AnalystGuy View Post
    Thank you! Yeah that worked perfectly. I know this is probably beyond what VBA can do but is there anyone to have it pre populated with a $ sign and then also as the user is typing it automatically formats the text in the Userform to display what is is going to be displayed on the document?
    Yes my form does this on my sheet...just gotta add a sub for that specific text box for when it changes so just double click the text box and put this:

    Private Sub SalesPrice_Change()
        SalesPrice.Value = Format(SalesPrice.Value, "$###,##")
    End Sub

    Now, to add a $ each time the form loads, you click the text box on your form and change the properties for "TEXT" and populate it with a "$" every time the form is loaded.
    Last edited by Aussiebear; 04-12-2023 at 06:19 PM. Reason: Added code tags

  5. #5
    That worked.

    Thanks again,

    AnalystGuy

  6. #6

    Currency Format

    Quote Originally Posted by igotgame View Post
    This is how I do it on one of my textboxes on a form (SalesPrice is my form textbox):

    SalesPrice.Value = Format(SalesPrice.Value, "$###,##")

    Then when they click Submit:

    Private Sub SubmitButton_Click()
    PurchasePrice = SalesPrice.Value
    End Sub

    I make PurchasePrice a Public Global variable so I can carry it over to my macro or macros from the form. So I mean this should work for what you need:

    PurchasePrice = Format(PurchasePrice, "$###,##")
    What is the proper currency format if we want to include 2 decimal points?
    Last edited by Aussiebear; 04-12-2023 at 06:19 PM. Reason: Adjusted the code tags

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I use

    $#,##0.00_);($#,##0.00);$0.00_);@

    positive;negative,zero;text

    the _) says to leave a )'s amount of space for the postitive so that the ($1,234.56) will align

    Textboxes show strings so I'd leave the actual dollar amount as a number (1234.56) and do the format at display time


    There's also the FOrmatCurrency function

    https://www.excelfunctions.net/vba-formatcurrency-function.html

    which I believe in international aware

    ---------------------------------------------------------------------------------------------------------------------

    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

Posting Permissions

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