Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 21

Thread: vba sum format number with currency

  1. #1

    vba sum format number with currency

    hi, experts
    i need help to sum format number with currency
    here i have userform textbox1 , 2 and 3
    the codes is :
    HTML Code:
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)TextBox1.Text = Format(TextBox1.Text, "#,##0.00")End Sub
    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)TextBox2.Text = Format(TextBox2.Text, "#,##0.00")End Sub
    Private Sub TextBox3_Change()TextBox3.Text = Format(TextBox3.Text, "$#,##0.00")End Sub


    when sum textbox1=1,300.00 and textbox2=1,500.00 it gives me textbox3=2.00 not 2,800.00
    and when i add the currency like this in textbox1,2,3 ="$#,##0.00" it gives me error
    i hope this help to under stand what i would

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Private Sub TextBox3_Change()
    TextBox3.Text = Format(TextBox3.Text, "$#,##0.00")
    End Sub
    That is correct code. The issue is in your math sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Quote Originally Posted by SamT View Post
    Private Sub TextBox3_Change()
    TextBox3.Text = Format(TextBox3.Text, "$#,##0.00")
    End Sub
    That is correct code. The issue is in your math sub

    can you explain more i need the solution

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Show us your math code that adds tbx1 and tbx2
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Quote Originally Posted by SamT View Post
    Show us your math code that adds tbx1 and tbx2
    this is my math code

    HTML Code:
    TextBox3.Value = Val(TextBox1) + Val(TextBox2)

  6. #6
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    Private Sub Recalculate()
      Dim ValTB1 As Double
      Dim ValTB2 As Double
      
      On Error Resume Next
      ValTB1 = CDbl(TextBox1.Value)
      ValTB2 = CDbl(TextBox2.Value)
      On Error GoTo 0
      
      TextBox3.Value = ValTB1 + ValTB2
    End Sub
    Artik

  7. #7
    Quote Originally Posted by Artik View Post
    Private Sub Recalculate()
      Dim ValTB1 As Double
      Dim ValTB2 As Double
      
      On Error Resume Next
      ValTB1 = CDbl(TextBox1.Value)
      ValTB2 = CDbl(TextBox2.Value)
      On Error GoTo 0
      
      TextBox3.Value = ValTB1 + ValTB2
    End Sub
    Artik

    thanks but it gives me $0.00
    attached my file
    Attached Files Attached Files

  8. #8
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    You changed the rules of the game during its duration.

    Remember that TextBox controls always store text. If you formatted the original number into a currency format, the TextBox contains the currency sign.
    Either you give up the currency symbol in TextBox1 and TextBox2, or you need to get rid of this symbol before making calculations.

    Artik

  9. #9
    Quote Originally Posted by Artik View Post
    You changed the rules of the game during its duration.

    Remember that TextBox controls always store text. If you formatted the original number into a currency format, the TextBox contains the currency sign.
    Either you give up the currency symbol in TextBox1 and TextBox2, or you need to get rid of this symbol before making calculations.

    Artik
    despite delete symbol of currency the problem is continued when i put theses values in textbox1,2 2,500 ,3500 it gives the result 5.00 not 6,000.00 you can try yourself

  10. #10
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    You can keep the Formats as in msg#1 and tweak 2 code lines in Artik's code in msg#6:
    ValTB1 = CDbl(Mid(TextBox1.Value, 2))
    ValTB2 = CDbl(Mid(TextBox2.Value, 2))
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  11. #11
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    Sorry, I suggested the US number format settings.
    If the format is other than US try this code:
    Private Sub CommandButton1_Click()
        Dim ValTB1      As Double
        Dim ValTB2      As Double
    
    
        On Error Resume Next
        ValTB1 = CDbl(TextBox1.Value)
        ValTB2 = CDbl(TextBox2.Value)
        On Error GoTo 0
    
    
        TextBox3.Value = Format(ValTB1 + ValTB2)
    End Sub
    
    
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        TextBox1.Text = Format(TextBox1.Text, "#,##0.00")
    End Sub
    
    
    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        TextBox2.Text = Format(TextBox2.Text, "#,##0.00")
    End Sub
    
    
    Private Sub TextBox3_Change()
        TextBox3.Text = Format(TextBox3.Text, "$#,##0.00")
    End Sub
    or another solution with the option of choosing your own currency symbol (in the example $) or national currency symbol (variable strCurCode).
    The example is a bit idiotic. I meant to show the possibility of using both symbols.
    Option Explicit
    
    
    Dim strCurCode      As String
    
    
    
    
    Private Sub CommandButton1_Click()
        Dim ValTB1      As Double
        Dim ValTB2      As Double
    
    
        On Error Resume Next
        ValTB1 = CDbl(ClearFormat(TextBox1.Value, "$"))
        ValTB2 = CDbl(ClearFormat(TextBox2.Value, "$"))
        On Error GoTo 0
        
        'here I used the national currency symbol (strCurCode)
        TextBox3.Value = f_CurrencyFormat(ValTB1 + ValTB2, strCurCode)
        'TextBox3.Value = f_CurrencyFormat(ValTB1 + ValTB2, "$")
    End Sub
    
    
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        TextBox1.Text = f_CurrencyFormat(TextBox1.Text, "$")
    End Sub
    
    
    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        TextBox2.Text = f_CurrencyFormat(TextBox2.Text, "$")
    End Sub
    
    
    
    
    Private Sub UserForm_Initialize()
        strCurCode = Application.International(xlCurrencyCode)
    End Sub
    
    
    
    
    Private Function f_CurrencyFormat(ByVal strVal As String, Optional CurrencySign As String) As String
        Dim dblVal      As Double
    
    
        strVal = Replace(strVal, CurrencySign, vbNullString)
        On Error Resume Next
        dblVal = CDbl(strVal)
        On Error GoTo 0
    
    
        f_CurrencyFormat = Format(dblVal, IIf(Len(CurrencySign) = 0, "", CurrencySign & " ") & "#,##0.00")
    
    
    End Function
    
    
    
    
    Private Function ClearFormat(ByVal strVal As String, Optional CurrencySign As String)
        ClearFormat = Replace(strVal, CurrencySign, vbNullString)
    End Function
    Artik

  12. #12
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I think you can simplify a bit



    Option Explicit
    
    
    Private Sub CommandButton1_Click()
        TextBox3.Value = FormatCurrency(CDbl(TextBox1.Value) + CDbl(TextBox2.Value))
    End Sub
    
    
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        TextBox1.Text = FormatCurrency(TextBox1.Text)
    End Sub
    
    
    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        TextBox2.Text = FormatCurrency(TextBox2.Text)
    End Sub
    Last edited by Paul_Hossler; 10-20-2019 at 11:24 AM.
    ---------------------------------------------------------------------------------------------------------------------

    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
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    All UserForm Code
    Option Explicit
    
    Dim ValTB1 As Double
    Dim ValTB2 As Double
    
    Private Sub CommandButton1_Click()
     TextBox3.Text = Format(ValTB1 + ValTB2, "$#,##0.00")
    End Sub
    
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    ValTB1 = CDbl(TextBox1)
    TextBox1.Text = Format(TextBox1.Text, "$#,##0.00")
    End Sub
    
    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    ValTB2 = CDbl(TextBox2)
    TextBox2.Text = Format(TextBox2.Text, "$#,##0.00")
    End Sub
    
    Private Sub UserForm_Click()
    
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  14. #14
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Don't use eventcode of elements to overcome the wrong choice of elements in a userform.
    Only adapt the results if necessary

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
      Sheet1.Cells(2) = 0 + TextBox1 + TextBox2
      Sheet1.Cells(2).NumberFormat = "€ 0.00"
    End Sub

  15. #15
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    FormatCurrency!
    I'd completely forgotten (or never knew) that one!
    Just one teeny tweak:
    TextBox3.Value = FormatCurrency(CDbl(TextBox1.Value) + CDbl(TextBox1.Value))
    to
    TextBox3.Value = FormatCurrency(CDbl(TextBox1.Value) + CDbl(TextBox2.Value))
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  16. #16
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    There's also FormatDateTime, FormatNumber, and FormatPercent

    I like them because they are aware of the computer's locale settings and can do some formatting

    PS - thanks for the teeny tweak


    https://docs.microsoft.com/en-us/off...6)%26rd%3Dtrue



    FormatCurrency function

    Returns an expression formatted as a currency value by using the currency symbol defined in the system control panel.
    Syntax

    FormatCurrency(Expression, [ NumDigitsAfterDecimal, [ IncludeLeadingDigit, [ UseParensForNegativeNumbers, [ GroupDigits ]]]])

    The FormatCurrency function syntax has these parts:
    Part Description
    Expression Required. Expression to be formatted.
    NumDigitsAfterDecimal Optional. Numeric value indicating how many places to the right of the decimal are displayed. Default value is -1, which indicates that the computer's regional settings are used.
    IncludeLeadingDigit Optional. Tristate constant that indicates whether or not a leading zero is displayed for fractional values. See Settings section for values.
    UseParensForNegativeNumbers Optional. Tristate constant that indicates whether or not to place negative values within parentheses. See Settings section for values.
    GroupDigits Optional. Tristate constant that indicates whether or not numbers are grouped by using the group delimiter specified in the computer's regional settings. See Settings section for values.
    ---------------------------------------------------------------------------------------------------------------------

    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

  17. #17
    Quote Originally Posted by Artik View Post
    Sorry, I suggested the US number format settings.
    If the format is other than US try this code:
    Private Sub CommandButton1_Click()
        Dim ValTB1      As Double
        Dim ValTB2      As Double
    
    
        On Error Resume Next
        ValTB1 = CDbl(TextBox1.Value)
        ValTB2 = CDbl(TextBox2.Value)
        On Error GoTo 0
    
    
        TextBox3.Value = Format(ValTB1 + ValTB2)
    End Sub
    
    thanks  you're both codes perfectly works 
    
    
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        TextBox1.Text = Format(TextBox1.Text, "#,##0.00")
    End Sub
    
    
    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        TextBox2.Text = Format(TextBox2.Text, "#,##0.00")
    End Sub
    
    
    Private Sub TextBox3_Change()
        TextBox3.Text = Format(TextBox3.Text, "$#,##0.00")
    End Sub
    or another solution with the option of choosing your own currency symbol (in the example $) or national currency symbol (variable strCurCode).
    The example is a bit idiotic. I meant to show the possibility of using both symbols.
    Option Explicit
    
    
    Dim strCurCode      As String
    
    
    
    
    Private Sub CommandButton1_Click()
        Dim ValTB1      As Double
        Dim ValTB2      As Double
    
    
        On Error Resume Next
        ValTB1 = CDbl(ClearFormat(TextBox1.Value, "$"))
        ValTB2 = CDbl(ClearFormat(TextBox2.Value, "$"))
        On Error GoTo 0
        
        'here I used the national currency symbol (strCurCode)
        TextBox3.Value = f_CurrencyFormat(ValTB1 + ValTB2, strCurCode)
        'TextBox3.Value = f_CurrencyFormat(ValTB1 + ValTB2, "$")
    End Sub
    
    
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        TextBox1.Text = f_CurrencyFormat(TextBox1.Text, "$")
    End Sub
    
    
    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        TextBox2.Text = f_CurrencyFormat(TextBox2.Text, "$")
    End Sub
    
    
    
    
    Private Sub UserForm_Initialize()
        strCurCode = Application.International(xlCurrencyCode)
    End Sub
    
    
    
    
    Private Function f_CurrencyFormat(ByVal strVal As String, Optional CurrencySign As String) As String
        Dim dblVal      As Double
    
    
        strVal = Replace(strVal, CurrencySign, vbNullString)
        On Error Resume Next
        dblVal = CDbl(strVal)
        On Error GoTo 0
    
    
        f_CurrencyFormat = Format(dblVal, IIf(Len(CurrencySign) = 0, "", CurrencySign & " ") & "#,##0.00")
    
    
    End Function
    
    
    
    
    Private Function ClearFormat(ByVal strVal As String, Optional CurrencySign As String)
        ClearFormat = Replace(strVal, CurrencySign, vbNullString)
    End Function
    Artik
    you're both code work perfectly

  18. #18
    Quote Originally Posted by Paul_Hossler View Post
    I think you can simplify a bit



    Option Explicit
    
    
    Private Sub CommandButton1_Click()
        TextBox3.Value = FormatCurrency(CDbl(TextBox1.Value) + CDbl(TextBox2.Value))
    End Sub
    
    
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        TextBox1.Text = FormatCurrency(TextBox1.Text)
    End Sub
    
    
    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        TextBox2.Text = FormatCurrency(TextBox2.Text)
    End Sub

    thanks paul your code well works

  19. #19
    Quote Originally Posted by SamT View Post
    All UserForm Code
    Option Explicit
    
    Dim ValTB1 As Double
    Dim ValTB2 As Double
    
    Private Sub CommandButton1_Click()
     TextBox3.Text = Format(ValTB1 + ValTB2, "$#,##0.00")
    End Sub
    
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    ValTB1 = CDbl(TextBox1)
    TextBox1.Text = Format(TextBox1.Text, "$#,##0.00")
    End Sub
    
    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    ValTB2 = CDbl(TextBox2)
    TextBox2.Text = Format(TextBox2.Text, "$#,##0.00")
    End Sub
    
    Private Sub UserForm_Click()
    
    End Sub
    your also code is excellent

  20. #20
    thanks so much for every one help me to solve my problem i really enjoyed with this discussing

Posting Permissions

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