?
Solved

Listbox calculations

Posted on 2005-03-22
1
Medium Priority
?
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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 Month11 days, 14 hours left to enroll

752 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