Solved

# Listbox calculations

Posted on 2005-03-22
Medium Priority
236 Views
hey Experts;

can anyone help me fix this code;  What it does now is it scans the list and if it finds all of the items that are the same and eliminates all of them but one but it keeps a count of all of them and puts it into the third column of my listbox.

instead of this i want it to do the same thing, eliminate all of the items that are the same but adds the value of the third column.  i don't know how to go about this.  here is the code i want to change.  Sorry if its confusing

Private Sub CalMisc()

Dim found As Boolean
Dim Scan1 As Long
Dim Scan2 As Long
Dim Count As Long

For Count = 0 To lstParts3.ListCount - 1
lstParts3.List(Count, 2) = 1
Next

Scan1 = lstParts3.ListCount - 1

Do
Scan2 = Scan1 - 1
found = False
Do
If lstParts3.List(Scan1) = lstParts3.List(Scan2) Then
lstParts3.List(Scan1, 2) = CLng(lstParts3.List(Scan1, 2)) + 1
lstParts3.RemoveItem Scan2
Scan1 = Scan1 - 1
found = True
End If

Scan2 = Scan2 - 1

Loop While Scan2 > 0

Loop While Scan1 > 0

End Sub
0
Question by:Jason_Kha
1 Comment

LVL 38

Accepted Solution

PaulHews earned 2000 total points
ID: 13602517
Adds the value of the third column to the existing item?  Try this

Private Sub EliminateDupes()

Dim i As Integer
Dim j As Integer
Dim lngTot As Long

With lstParts3
For i = 0 To .ListCount - 1
For j = .ListCount - 1 To i Step -1
If GetCol(i, 1) = GetCol(j, 1) And i <> j Then
lngTot = CLng(GetCol(i, 3)) + CLng(GetCol(j, 3))
SetCol i, 3, CStr(lngTot)
.RemoveItem j

End If
Next j

Next i
End With

End Sub

Private Function GetCol(Item As Integer, Column As Integer) As String
Dim strSplit() As String

With lstParts3
If Item >= 0 And Item <= .ListCount - 1 Then
If Column = 1 Then
GetCol = .List(Item)
Else
GetCol = .List(Item, Column - 1)
End If
End If

End With
End Function

Private Sub SetCol(Item As Integer, Column As Integer, Value As String)
Dim strSplit() As String

With lstParts3
If Item >= 0 And Item <= .ListCount - 1 Then
If Column = 1 Then
.List(Item) = Value
Else
.List(Item, Column - 1) = Value
End If

End If
End With

End Sub
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month8 days, 7 hours left to enroll