[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

Best Approach (psuedocode) for summing structure element values

David

8/18/2010 2:26:00 AM

I'm dealing with an Excel worksheet which allow the user to enter
a values into a year/date column and an amount column. If the
user enters multiple years then I need to sum the values.

I thought this might be easier in VB so what I had in mind was:

1) Create two identical structures (type) of:

Type TWSInfo
thisYr As Integer
thisValue As Single
End type

Dim TThisWS(rowcount) as TWSInfo
Dim TThisSum(rowcount) As TWSInfo

2) Read in each Excel row into the structure

3) Sort the structure by Year

4) Loop the structure array comparing the next "thisYr" element to the
previous "thisYr" element, and if the years match, summing the values

5 ) Storing the Yr and summed value in the structure TThisSum

======================================

Anyone have an easier / better way?

Thanks
David





4 Answers

Ice Foot

8/18/2010 3:54:00 AM

0


"David" <NoWhere@earthlink.net> wrote in message
news:ukBb5ynPLHA.620@TK2MSFTNGP06.phx.gbl...
> I'm dealing with an Excel worksheet which allow the user to enter
> a values into a year/date column and an amount column. If the
> user enters multiple years then I need to sum the values.
>
> I thought this might be easier in VB so what I had in mind was:
>
> 1) Create two identical structures (type) of:
>
> Type TWSInfo
> thisYr As Integer
> thisValue As Single
> End type
>
> Dim TThisWS(rowcount) as TWSInfo
> Dim TThisSum(rowcount) As TWSInfo
>
> 2) Read in each Excel row into the structure
>
> 3) Sort the structure by Year
>
> 4) Loop the structure array comparing the next "thisYr" element to the
> previous "thisYr" element, and if the years match, summing the values
>
> 5 ) Storing the Yr and summed value in the structure TThisSum
>
> ======================================
>
> Anyone have an easier / better way?
>
> Thanks
> David
>
>
>
>
>

i don't know about easier or better, but one alternative is use a
cSortedDictionary (Olaf Schmidt's dhRichClient3.dll)
use year as key, (automatically sorted),...something like
'pseudocode
Private mSumDict as cSortedDictionary

Sub StoreYearSum(year as string, sum as single)
if mSumDict is Nothing then set mSumDict = New cSortedDictionary
if mSumDict.Exists (year) then
mSumDict.Item(year) = mSumDict.Item(year) + sum
else
mSumDict.Add(year,sum)
End if
End Sub

Sub ReadTotals()
dim i as long
for i = 0 to mSumDict.count -1
debug.print "Year", mSumDict.KeyByIndex(i), "Sum",
mSumDict.ItemByIndex(i)
next i
End Sub

fwiw
mark


David

8/18/2010 3:27:00 PM

0

Thanks for response mark.

Not familiar with (Olaf Schmidt's dhRichClient3.dll)
but will search the net and check it out.

David



"mp" <nospam@Thanks.com> wrote in message
news:i4flh2$3mh$1@news.eternal-september.org...
>
> "David" <NoWhere@earthlink.net> wrote in message
> news:ukBb5ynPLHA.620@TK2MSFTNGP06.phx.gbl...
>> I'm dealing with an Excel worksheet which allow the user to enter
>> a values into a year/date column and an amount column. If the
>> user enters multiple years then I need to sum the values.
>>
>> I thought this might be easier in VB so what I had in mind was:
>>
>> 1) Create two identical structures (type) of:
>>
>> Type TWSInfo
>> thisYr As Integer
>> thisValue As Single
>> End type
>>
>> Dim TThisWS(rowcount) as TWSInfo
>> Dim TThisSum(rowcount) As TWSInfo
>>
>> 2) Read in each Excel row into the structure
>>
>> 3) Sort the structure by Year
>>
>> 4) Loop the structure array comparing the next "thisYr" element to the
>> previous "thisYr" element, and if the years match, summing the values
>>
>> 5 ) Storing the Yr and summed value in the structure TThisSum
>>
>> ======================================
>>
>> Anyone have an easier / better way?
>>
>> Thanks
>> David
>>
>>
>>
>>
>>
>
> i don't know about easier or better, but one alternative is use a
> cSortedDictionary (Olaf Schmidt's dhRichClient3.dll)
> use year as key, (automatically sorted),...something like
> 'pseudocode
> Private mSumDict as cSortedDictionary
>
> Sub StoreYearSum(year as string, sum as single)
> if mSumDict is Nothing then set mSumDict = New cSortedDictionary
> if mSumDict.Exists (year) then
> mSumDict.Item(year) = mSumDict.Item(year) + sum
> else
> mSumDict.Add(year,sum)
> End if
> End Sub
>
> Sub ReadTotals()
> dim i as long
> for i = 0 to mSumDict.count -1
> debug.print "Year", mSumDict.KeyByIndex(i), "Sum",
> mSumDict.ItemByIndex(i)
> next i
> End Sub
>
> fwiw
> mark
>


Ice Foot

8/18/2010 7:49:00 PM

0

http://www.thecommon....
to download olafs' dlls
the archives from this group will also have some previous
references/questions/answers

"David" <NoWhere@earthlink.net> wrote in message
news:uKXRBnuPLHA.620@TK2MSFTNGP06.phx.gbl...
> Thanks for response mark.
>
> Not familiar with (Olaf Schmidt's dhRichClient3.dll)
> but will search the net and check it out.
>
> David
>
>
>
> "mp" <nospam@Thanks.com> wrote in message
> news:i4flh2$3mh$1@news.eternal-september.org...
>>
>> "David" <NoWhere@earthlink.net> wrote in message
>> news:ukBb5ynPLHA.620@TK2MSFTNGP06.phx.gbl...
>>> I'm dealing with an Excel worksheet which allow the user to enter
>>> a values into a year/date column and an amount column. If the
>>> user enters multiple years then I need to sum the values.
>>>
>>> I thought this might be easier in VB so what I had in mind was:
>>>
>>> 1) Create two identical structures (type) of:
>>>
>>> Type TWSInfo
>>> thisYr As Integer
>>> thisValue As Single
>>> End type
>>>
>>> Dim TThisWS(rowcount) as TWSInfo
>>> Dim TThisSum(rowcount) As TWSInfo
>>>
>>> 2) Read in each Excel row into the structure
>>>
>>> 3) Sort the structure by Year
>>>
>>> 4) Loop the structure array comparing the next "thisYr" element to the
>>> previous "thisYr" element, and if the years match, summing the
>>> values
>>>
>>> 5 ) Storing the Yr and summed value in the structure TThisSum
>>>
>>> ======================================
>>>
>>> Anyone have an easier / better way?
>>>
>>> Thanks
>>> David
>>>
>>>
>>>
>>>
>>>
>>
>> i don't know about easier or better, but one alternative is use a
>> cSortedDictionary (Olaf Schmidt's dhRichClient3.dll)
>> use year as key, (automatically sorted),...something like
>> 'pseudocode
>> Private mSumDict as cSortedDictionary
>>
>> Sub StoreYearSum(year as string, sum as single)
>> if mSumDict is Nothing then set mSumDict = New cSortedDictionary
>> if mSumDict.Exists (year) then
>> mSumDict.Item(year) = mSumDict.Item(year) + sum
>> else
>> mSumDict.Add(year,sum)
>> End if
>> End Sub
>>
>> Sub ReadTotals()
>> dim i as long
>> for i = 0 to mSumDict.count -1
>> debug.print "Year", mSumDict.KeyByIndex(i), "Sum",
>> mSumDict.ItemByIndex(i)
>> next i
>> End Sub
>>
>> fwiw
>> mark
>>
>
>


David

8/19/2010 7:52:00 PM

0

Thanks mp -- found it.
Don't really like using someones dll where I don't have source.
But nice to know its available.

Consider this thread closed.
Have a nice day.
David

"mp" <nospam@Thanks.com> wrote in message
news:i4hdf1$rii$1@news.eternal-september.org...
> http://www.thecommon....
> to download olafs' dlls
> the archives from this group will also have some previous
> references/questions/answers
>
> "David" <NoWhere@earthlink.net> wrote in message
> news:uKXRBnuPLHA.620@TK2MSFTNGP06.phx.gbl...
>> Thanks for response mark.
>>
>> Not familiar with (Olaf Schmidt's dhRichClient3.dll)
>> but will search the net and check it out.
>>
>> David
>>
>>
>>
>> "mp" <nospam@Thanks.com> wrote in message
>> news:i4flh2$3mh$1@news.eternal-september.org...
>>>
>>> "David" <NoWhere@earthlink.net> wrote in message
>>> news:ukBb5ynPLHA.620@TK2MSFTNGP06.phx.gbl...
>>>> I'm dealing with an Excel worksheet which allow the user to enter
>>>> a values into a year/date column and an amount column. If the
>>>> user enters multiple years then I need to sum the values.
>>>>
>>>> I thought this might be easier in VB so what I had in mind was:
>>>>
>>>> 1) Create two identical structures (type) of:
>>>>
>>>> Type TWSInfo
>>>> thisYr As Integer
>>>> thisValue As Single
>>>> End type
>>>>
>>>> Dim TThisWS(rowcount) as TWSInfo
>>>> Dim TThisSum(rowcount) As TWSInfo
>>>>
>>>> 2) Read in each Excel row into the structure
>>>>
>>>> 3) Sort the structure by Year
>>>>
>>>> 4) Loop the structure array comparing the next "thisYr" element to the
>>>> previous "thisYr" element, and if the years match, summing the
>>>> values
>>>>
>>>> 5 ) Storing the Yr and summed value in the structure TThisSum
>>>>
>>>> ======================================
>>>>
>>>> Anyone have an easier / better way?
>>>>
>>>> Thanks
>>>> David
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>> i don't know about easier or better, but one alternative is use a
>>> cSortedDictionary (Olaf Schmidt's dhRichClient3.dll)
>>> use year as key, (automatically sorted),...something like
>>> 'pseudocode
>>> Private mSumDict as cSortedDictionary
>>>
>>> Sub StoreYearSum(year as string, sum as single)
>>> if mSumDict is Nothing then set mSumDict = New cSortedDictionary
>>> if mSumDict.Exists (year) then
>>> mSumDict.Item(year) = mSumDict.Item(year) + sum
>>> else
>>> mSumDict.Add(year,sum)
>>> End if
>>> End Sub
>>>
>>> Sub ReadTotals()
>>> dim i as long
>>> for i = 0 to mSumDict.count -1
>>> debug.print "Year", mSumDict.KeyByIndex(i), "Sum",
>>> mSumDict.ItemByIndex(i)
>>> next i
>>> End Sub
>>>
>>> fwiw
>>> mark
>>>
>>
>>
>
>