Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Sum textboxes in a collection

Any help for a newbie would be appreciated. I'm trying to sum several
texbox inputs on a form and display the result in a "totals" textbox on the
same form. The textboxes are part of a collection.

The following code concatenates the values in the textbox inputs rather than
adding the values:
Global declarations:
Dim textBoxes as New Collection
Dim textboxNames() as String

Private Sub txtCash_AfterUpdate()

'update total allocation
AllocationTotal

End Sub


Sub AllocationTotal()
Dim i As Integer
Dim fieldVal, boxSum As Variant

txtTotalAllocation.Value = 0

For i = 2 To (totalFields - 1)

fieldVal = textBoxes(textboxNames(i - 1)).Text
boxSum = boxSum + fieldVal

Next 'i

txtTotalAllocation.Value = boxSum

End Sub

How can I change this code so that the values in each textbox are summed
instead of concatenated?

Thanks in advance,
Mark


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Sum textboxes in a collection

Sub AllocationTotal()
Dim i As Integer
Dim fieldVal, boxSum As Double

txtTotalAllocation.Value = 0

For i = 2 To (totalFields - 1)

fieldVal = textBoxes(textboxNames(i - 1)).Text
if isnumeric(fieldVal) then
boxSum = boxSum + cdbl(fieldVal)
end if

Next 'i

txtTotalAllocation.Value = Format(boxSum,"#.00")

End Sub

If they will be integers, then change Double to Long, cdbl to clng
Format to Format(boxSum,"0")

--
Regards,
Tom Ogilvy


Mark wrote in message
...
Any help for a newbie would be appreciated. I'm trying to sum several
texbox inputs on a form and display the result in a "totals" textbox on

the
same form. The textboxes are part of a collection.

The following code concatenates the values in the textbox inputs rather

than
adding the values:
Global declarations:
Dim textBoxes as New Collection
Dim textboxNames() as String

Private Sub txtCash_AfterUpdate()

'update total allocation
AllocationTotal

End Sub


Sub AllocationTotal()
Dim i As Integer
Dim fieldVal, boxSum As Variant

txtTotalAllocation.Value = 0

For i = 2 To (totalFields - 1)

fieldVal = textBoxes(textboxNames(i - 1)).Text
boxSum = boxSum + fieldVal

Next 'i

txtTotalAllocation.Value = boxSum

End Sub

How can I change this code so that the values in each textbox are summed
instead of concatenated?

Thanks in advance,
Mark




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
working with textboxes-VBA zxc[_2_] New Users to Excel 0 June 2nd 10 07:38 AM
Textboxes SAL Excel Discussion (Misc queries) 2 July 13th 07 12:24 AM
Reference Tab name to two textboxes AOU Excel Discussion (Misc queries) 5 December 8th 06 09:57 AM
Looping through textboxes CLamar Excel Discussion (Misc queries) 1 July 12th 06 04:33 PM
tab between several textboxes Kim Excel Worksheet Functions 0 May 9th 05 04:08 PM


All times are GMT +1. The time now is 11:35 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"