?
Solved

Listbox calculations

Posted on 2005-03-22
1
Medium Priority
?
236 Views
Last Modified: 2010-05-02
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

If (Not found) Then Scan1 = Scan1 - 1

Loop While Scan1 > 0

End Sub
0
Comment
Question by:Jason_Kha
1 Comment
 
LVL 38

Accepted Solution

by:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Article by: Martin
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

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question