Consulting

Results 1 to 9 of 9

Thread: mismatch when sum values in multiple textboxes

  1. #1

    mismatch when sum values in multiple textboxes

    Hi
    I try summing values for multiple textboxes from 11 to 20 and put the total in textbox21 with considering each textbox contains numberformat "#,##0.00"
    it gives me error mismatch in this line
     Me.TextBox21.Value = CDbl(Me.TextBox21.Value) + _
     CDbl(Replace(Me.Controls("TextBox" & i).Value, ",", ""))
    Dim i As Long
    For i = 11 To 20
        Me.TextBox21.Value = CDbl(Me.TextBox21.Value) + _
     CDbl(Replace(Me.Controls("TextBox" & i).Value, ",", ""))
    Next
    any help to fix it or alternative,please?

  2. #2
    Dim i As Integer
    Dim result As Double, tmp As String
    result = Val(Me.Textbox21 & "")
    For i = 11 To 20
        tmp = Trim$(Me.Controls("TextBox" & i).Value & "")
        If Len(tmp) Then
            result = result + CCur(tmp)
        End If
    Next
    Me.TextBox21 = result

  3. #3
    thanks , it doesn't show right values . look at picture
    Attached Images Attached Images

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    For j = 11 To 20
      y = y+ CDbl(Me("TextBox" & j))
    Next
    msgbox y

  5. #5
    @snb thanks still shows the same error in this line
      y = y+ CDbl(Me("TextBox" & j))

  6. #6
    but in my userform it is calculating correctly?
    Attached Files Attached Files

  7. #7
    @arnelgp thanks , based on your file yes but there is problem . it sums repeatedly . for instance if I have in textbox11,12= 12+12 from the first time will be 24 but if I press again will be 48 I don't want to sum repeatedly just from the first time should be based what is existed in textboxes . not every time add & sum over previous value as your code does it .

  8. #8
    you remove (comment out) the first "result" from the code:
    Dim i As Integer
    Dim result As Double, tmp As String
    For i = 11 To 20
        tmp = Trim$(Me.Controls("TextBox" & i).Value & "")
        If Len(tmp) Then
            result = result + CCur(tmp)
        End If
    Next
    Me.TextBox21 = result

  9. #9
    thanks now it works.

Posting Permissions

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