Combining Data in a List

Does anyone here knows how to combine data from 1 list in a document to another list in another documentList1 and List2 is a text field that accepts multiple value

example is

List 1 from doc1

Qty Unit Item

3 pcs Folder

2 pcs ballpen

3 pcs paper

List 2 from doc2

Qty Unit Item

3 pcs paper

2 pcs ballpen

3 pcs folder

Output to doc3

Qty Unit Item

6 pcs Folder

4 pcs ballpen

6 pcs paper

Subject: RE: Combining Data in a List

Class QuantityTotaler

Private QtyList List As Double



Sub AddLines(varVals As Variant)

	Dim dblQty As Double

	Dim strOfWhat As String

	Dim strLine As String

	

	Forall valu In varVals

		strLine = Fulltrim(valu)

		dblQty = Cdbl(Strleft(strLine, " "))

		strOfWhat = Strright(strLine, " ")

		If Iselement(QtyList(strOfWhat)) Then

			QtyList(strOfWhat) = QtyList(strOfWhat) + dblQty

		Else

			QtyList(strOfWhat) = dblQty

		End If

	End Forall

End Sub



Public Property Get Result As Variant

	Dim strOutput As String

	Forall qty In QtyList

		strOutput = strOutput & {

} & qty & " " & Listtag(qty)

	End Forall

	Result = Split(Mid$(strOutput, 2), {

})

End Property

End Class

Dim adder As New QuantityTotaler

adder.AddLines(doc1.fieldname(0))

adder.AddLines(doc2.fieldname(0))

doc3.fieldname = adder.Result

Of course, this only works insofar as the same item is always listed with the same unit of measurement. If instead of “pcs” always, you have some “cups” and some “gallons” of the same thing, this code doesn’t know how to combine those into a single line.

Subject: RE: Combining Data in a List

Does this code adds if the value is the same with the other list? or does it sum all for list 1 and adds total of list2?

Coz what i need to know is get list1, then get list2 and run thru each items in list2 comparing each items to list1 and adds if found a match, otherwise i need to append the data in list2 that is not listed in list1

Subject: It does what you asked for. Try it and see.

The QtyList is a List value, which means that it contains a collection of strings with associated values of a specified type (in this case, “Double” floating point numbers). In this case the string values are the what you have a quantity of and the Double values are how much.

Subject: RE: It does what you asked for. Try it and see.

Thanks, that one really works, i configure a little to suit my needs.

Sorry for the late reply.

Glenn