View Full Version : [SOLVED:] mismatch when sum values in multiple textboxes
maghari
08-11-2022, 03:28 AM
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?
arnelgp
08-11-2022, 05:43 AM
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
maghari
08-11-2022, 09:28 AM
thanks , it   doesn't  show  right  values . look at picture
For j = 11 To 20
  y = y+ CDbl(Me("TextBox" & j))
Next
msgbox y
maghari
08-12-2022, 02:11 PM
@snb thanks  still shows  the  same error in this  line 
  y = y+ CDbl(Me("TextBox" & j))
arnelgp
08-12-2022, 05:51 PM
but in my userform it is calculating correctly?
maghari
08-13-2022, 01:35 AM
@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   .
arnelgp
08-13-2022, 01:40 AM
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
maghari
08-16-2022, 06:11 AM
thanks now  it  works.:yes
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.