Calculation using list box items
Hi,
I am benefited by this forum to a very great extent. Now for the past two days trying to do some calculations in the List Box items. I have two list boxes and the first list box contains the contents of a text file. Once the user clicks the item in List Box 1 same is added in List Box 2 and removed from List Box 1. This I am able to do without any difficulty. But I need to count the number of items added in List Box 2 and also the sum of a string in the List Box 2. I am pasting the code to this mail. Kindly advise me in this regard.
Code:
Private Sub lstDisplayrecord_Click()
Dim Searchstring As String
Dim Reasoncode As String
Dim newstring As String
Dim Linecnt As Integer
Dim Totamt As String
Searchstring = lstDisplayrecord.List(lstDisplayrecord.ListIndex)
lblAccount = Mid(Searchstring, 10, 2)
lblAccnum = Mid(Searchstring, 12, 15)
lblTrandate = Format(Mid(Searchstring, 141, 8), "00/00/0000")
lblName = Mid(Searchstring, 27, 40)
lblAmount = Format(Mid(Searchstring, 116, 15) * 0.01, "#############.00")
Reasoncode = InputBox("Enter Reason Code", "Reason Code", "01")
'If Reasoncode = "01" Or "02" Or "03" Or "04" Or "05" Then
Totamt = Totamt + lblAmount
Searchstring = Left(Searchstring, Len(Searchstring) - 2) & Reasoncode
lstrejectecs.AddItem Searchstring
lblrejecsamt = Format(Totamt, "#############.00")
Linecnt = Linecnt + 1
lblrejecsnum = Linecnt
lstDisplayrecord.RemoveItem lstDisplayrecord.ListIndex
End Sub
Re: Calculation using list box items
To get a count of what is in List2To add up numbers in a listbox
Code:
Private Sub Command1_Click()
Dim i As Long
Dim lngTotal As Long
For i = 0 To List1.ListCount - 1
lngTotal = lngTotal + CLng(List1.List(i))
Next
MsgBox lngTotal
End Sub
Re: Calculation using list box items
Hi Hack,
While I have changed at necessary places (like List1 to whatever lst I have used). But it gives error with CLng. I am displaying a full line of a text file in the List 1 and I want to calculate the sum of a string (I am using Mid(Searchstring,116,15) the place of the string in that line of the text file. Kindly help me please.
Re: Calculation using list box items
try
vb Code:
clng(Mid(Searchstring,116,15))
Re: Calculation using list box items
Hi,
Thanks it is working fine. Now I am facing with a different problem in the calculation. I am calculating the totals of two ListBox items. I have declared the variables and it works fine. But when the total number of records are equal in both the lists then it gives an error "Runtime Error 13" Type Mismatch.
For example:
Total 8 items are there in Listbox1
I select 3 items and add it to Listbox2
I get the total of amount column(string) of both the Listbox1 and Listbox2
But as soon as I select the 4th item in Listbox1 then I get error Type Mismatch.
I tried with a different file which was containing 96 records. Exactly after 47 records I got the same error.
Kindly tell me where I am going wrong.
Re: Calculation using list box items
yes you need to stop at the end of the listbox with the least items, as listbox 2 probably has the least items use its count as the limit
Re: Calculation using list box items
And make sure you add some code to check if the ListCount = 0