Hello Guys.. Need help again with this code :(

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim Name As String
        Dim Visits, Calls As Integer
        Dim Sales As Long
        Visits = 0
        Calls = 0
        Sales = 0

        'I want to store all the lvAccountManager Column 1 a string called "Name"
        For Each itm As ListViewItem In Me.lvAccountManager.Items
            Name = itm.SubItems(1).Text
        Next

        'if the "Name" has a value they will find it in Listview1
        'It will add all the Columns 3 if they are part of the "Name"

        For Each Itm As ListViewItem In ListView1.Items
            If Itm.SubItems(1).Text = Name Then
                Visits += Val(Itm.SubItems(2).Text)
                Calls += Val(Itm.SubItems(3).Text)
            End If
        Next

        'Same goes here *Take note its also a different listview.
        For Each Itm As ListViewItem In lv.Items
            If Itm.SubItems(2).Text = Name Then
                Sales += Val(Itm.SubItems(9).Text)
            End If
        Next

        'The result will be like this
        will be added to a new listview...

        For Each Total As Totals In Activity
            Me.ListView2.Items.Add(Total.Name, Total.Name, 0)
            Me.ListView2.Items(Total.Name).SubItems.Add(Str(Total.AverageSales))
            Me.ListView2.Items(Total.Name).SubItems.Add(Str(Total.Calls))
            Me.ListView2.Items(Total.Name).SubItems.Add(Str(Total.Visits))
        Next

    End Sub

This is the example Output
Imagine this is a listview Lol

              ___________________________________
             /  Name   / Sales  / Calls / Visits /
            / Sample  /  1000  /   20  /   10   /
           / Sample2 /   20   /   15  /    8   /

Please help me :(

What problems are you getting with this code?

It seems that its wrong Loop statement is not appropriate in getting the "Name" string.

Variable Name is used before it has been asigned a value.

Variable Name is used before it has been asigned a value.

try changing line 3 to Dim Name As String = "" this will initialize the Name variable.

'I want to store all the lvAccountManager Column 1 a string called "Name"

Line 12 - Name = Name & itm.SubItems(1).Text. This will add each name to Name and you'll end up with one long string of names end to end.

Lines 19 & 27 - If Name.Contains(Itm.SubItems(1).Text) Then This is line 19 but follow this pattern for 27 as well. This searches through that long string of names for a particular name and returns true if it's found, false if it isn't.

hi @tinstaafl i will try it right now jsut give me 5 minutes :)

Uhmm.. how i supposed to add it on Listview3?
using in loop?

did you keep that code I did for you before? It had all the steps there.

I got this when i click the button

tanstaffl1

It seems that the "Name" store all the the value in my first row.
and it didn't loop all the details.

    ' start code from tanstaffi

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim Name As String = ""
        Dim Visits, Calls As Integer
        Dim Sales As Long
        Visits = 0
        Calls = 0
        Sales = 0

        'I want to store all the lvAccountManager Column 1 a string called "Name"
        For Each itm As ListViewItem In Me.lvAccountManager.Items
            Name = Name & itm.SubItems(1).Text
        Next

        'if the "Name" has a value they will find it in Listview1
        'It will add all the Columns 3 if they are part of the "Name"

        For Each Itm As ListViewItem In ListView1.Items
            If Name.Contains(Itm.SubItems(1).Text) Then
                Visits += Val(Itm.SubItems(2).Text)
                Calls += Val(Itm.SubItems(3).Text)
            End If
        Next

        'Same goes here *Take note its also a different listview.

        For Each Itm As ListViewItem In lv.Items
            If Name.Contains(Itm.SubItems(1).Text) Then
                Sales += Val(Itm.SubItems(9).Text)
            End If
        Next

        For Each Items In Name
            ListView2.Items.Add(Name, Name, 0)
            ListView2.Items(Name).SubItems.Add(Str(Sales))
            ListView2.Items(Name).SubItems.Add(Str(Calls))
            ListView2.Items(Name).SubItems.Add(Str(Visits))
        Next

    End Sub

I use this code.
:(

I took another look at my code and compared it to what you want. I realized it would require quite a few changes so I went ahead and made them for you. The code is pretty basic, but since I didn't have all the data I didn't run it. One thing I'm not sure of is your numbering for the subitems. Item.Subitem(0) is the first column basically it's the same as Item.Text. The next column would start numbering with 1. You might have to change some of the numbers I put in there to reflect this. I made an assumption and added a total sales field and a sales transaction counter so that everytime a sale was added to a name I increased the number of sales by 1. This way dividing sales by the number of sales gets the average sale. Not sure if this is what you needed but the code is fairly basic and I added some comments for you. You should be able to tweak it to fit.

Public Class TotalListviewColuimn
    'This structure will hold all the info you want to consolidate
    Structure Totals
        Dim Name As String
        Dim NumSales As Integer
        Dim Sales As Long
        Dim Visits As Integer
        Dim Calls As Integer
    End Structure
    'This list will hold the totals of the activity of all the sales people
    Dim Activity As New List(Of Totals)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GetTotals()
        'Add all the totals to a new listview
        For Each Total As Totals In Activity
            ListView3.Items.Add(Total.Name, Total.Name, 0)
            ListView3.Items(Total.Name).SubItems.Add(Str(Total.Calls))
            ListView3.Items(Total.Name).SubItems.Add(Str(Total.Visits))
            ListView3.Items(Total.Name).SubItems.Add(Str(Total.Sales / Total.NumSales))
        Next

    End Sub
    Public Sub GetTotals()
        Dim TempTotal As Totals
        Dim Indx As Integer
        With TempTotal
            .Name = ""
            .Visits = 0
            .Calls = 0
        End With
        'Load each name from lvAccountManager into the Activity list
        For Each Itm As ListViewItem In Me.lvAccountManager.Items
            'Check if the name already exists.  
            'CheckName returns -1 if false, otherwise we ignore it.
            'We only want 1 instance of each name in Activity, 
            'as it will hold the totals
            Indx = CheckActivityForName(Itm.Text)
            If Indx = -1 Then
                With TempTotal
                'initialize the fields in the activity list
                    .Name = Itm.Text
                    .Sales = 0
                    .Calls = 0
                    .Visits = 0
                    .NumSales = 0
                End With
                Activity.Add(TempTotal)
            End If
        Next
        For Each Total As Totals In Activity
            'Get total visits and calls for each name we put into Activity
            For Each Itm As ListViewItem In ListView1.Items
                If Itm.Text = Total.Name Then
                    Total.Visits += Val(Itm.SubItems(1).Text)
                    Total.Calls += Val(Itm.SubItems(2).Text)
                End If
            Next
            'Get total sales and transactions for each name we put into Activity
            For Each Itm As ListViewItem In lv.Items
                If Itm.Text = Total.Name Then
                    'Average sales will be Sales/NumSales
                    Total.Sales += Val(Itm.SubItems(9).Text)
                    Total.NumSales += 1
                End If
            Next
        Next
    End Sub
    Public Function CheckActivityForName(ByVal Name As String) As Integer
        'false is the default
        CheckActivityForName = -1
        For Each Total As Totals In Activity
            'true returns the index of the item
            If Name = Total.Name Then
                CheckActivityForName = Activity.IndexOf(Total)
                Return CheckActivityForName
            End If
        Next
    End Function
End Class

If you have any questions on what I did let me know.

Hi @tinstaafl it seems that it didnt show any details.

    'This structure will hold all the info you want to consolidate
    Structure Totals
        Dim Name As String
        Dim NumSales As Integer
        Dim Sales As Long
        Dim Visits As Integer
        Dim Calls As Integer
    End Structure
    'This list will hold the totals of the activity of all the sales people
    Dim Activity As New List(Of Totals)
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        GetTotals()
        'Add all the totals to a new listview
        For Each Total As Totals In Activity
            ListView2.Items.Add(Total.Name, Total.Name, 0)
            ListView2.Items(Total.Name).SubItems.Add(Str(Total.Calls))
            ListView2.Items(Total.Name).SubItems.Add(Str(Total.Visits))
            ListView2.Items(Total.Name).SubItems.Add(Str(Total.Sales))
            ListView2.Items(Total.Name).SubItems.Add(Str(Total.Sales / Total.NumSales))
        Next
    End Sub

    Public Sub GetTotals()
        Dim TempTotal As Totals
        Dim Indx As Integer
        With TempTotal
            .Name = ""
            .Visits = 0
            .Calls = 0
        End With
        'Load each name from lvAccountManager into the Activity list
        For Each Itm As ListViewItem In Me.lvAccountManager.Items
            'Check if the name already exists.
            'CheckName returns -1 if false, otherwise we ignore it.
            'We only want 1 instance of each name in Activity,
            'as it will hold the totals
            Indx = CheckActivityForName(Itm.SubItems(1).Text)
            If Indx = -1 Then
                With TempTotal
                    'initialize the fields in the activity list
                    .Name = Itm.SubItems(1).Text
                    .Sales = 0
                    .Calls = 0
                    .Visits = 0
                    .NumSales = 0
                End With
                Activity.Add(TempTotal)
            End If
        Next
        For Each Total As Totals In Activity
            'Get total visits and calls for each we put into Activity
            For Each Itm As ListViewItem In ListView1.Items
                If Itm.SubItems(1).Text = Total.Name Then
                    Total.Visits += Val(Itm.SubItems(1).Text)
                    Total.Calls += Val(Itm.SubItems(2).Text)
                End If
            Next
            'Get total sales and transactions for each name
            For Each Itm As ListViewItem In lv.Items
                If Itm.SubItems(1).Text = Total.Name Then
                    'Average sales will be Sales/NumSales
                    Total.Sales += Val(Itm.SubItems(9).Text)
                    Total.NumSales += 1
                End If
            Next
        Next
    End Sub

    Public Function CheckActivityForName(ByVal Name As String) As Integer
        'false is the default
        CheckActivityForName = -1
        For Each Total As Totals In Activity
            'true returns the index of the item
            If Name = Total.Name Then
                CheckActivityForName = Activity.IndexOf(Total)
                Return CheckActivityForName
            End If
        Next
    End Function

I put itm.Subitems(1).Text because this is the column they going to match in every Listview.
full_data

Maybe the value of Name did not match so thats why i didnt give a detail?
but i dont see the problem in this code...

Line 54 the index should be 3. Your picture doesn't show the sales column but I'll assume you got it right, just in case, column number 10 will have index 9. It took some figuring but I figured it out here's the revised code. I think I got the subitems indexes right:

   Public Class TotalListviewColuimn
        Structure Totals
            Dim Name As String
            Dim NumSales As Integer
            Dim Sales As Long
            Dim Visits As Integer
            Dim Calls As Integer
        End Structure
        Dim Activity As New List(Of Totals)
        Dim AcctMgrs As New List(Of String)

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            GetTotals()
            For Each Total As Totals In Activity
                ListView3.Items.Add(Total.Name, Total.Name, 0)
                ListView3.Items(Total.Name).SubItems.Add(Str(Total.Calls))
                ListView3.Items(Total.Name).SubItems.Add(Str(Total.Visits))
                ListView3.Items(Total.Name).SubItems.Add(Str(Total.Sales / Total.NumSales))
            Next

        End Sub
        Public Sub GetTotals()
            Dim TempTotal As Totals
            Dim Indx As Integer
            With TempTotal
                .Name = ""
                .Visits = 0
                .Sales = 0
                .Calls = 0
                .NumSales = 0
            End With
            'Load each name fro lvAccountManager into the Activity list
            'and the AcctMgr list
            For Each Itm As ListViewItem In Me.lvAccountManager.Items
                'Check if the name already exists.  
                'CheckName returns -1 if false, otherwise we ignore it.
                'We only want 1 instance of each name in Activity, 
                'as it will hold the totals
                Indx = CheckActivityForName(Itm.SubItems(1).Text)
                If Indx = -1 Then
                    With TempTotal
                        .Name = Itm.SubItems(1).Text
                        .Sales = 0
                        .Calls = 0
                        .Visits = 0
                        .NumSales = 0
                    End With
                    AcctMgrs.Add(Itm.Text)
                    Activity.Add(TempTotal)
                End If
            Next
            For Each Name As String In AcctMgrs
                'Get total visits and calls for each name we put into Activity
                For Each Itm As ListViewItem In ListView1.Items
                    If Itm.SubItems(1).Text = Name Then
                        TempTotal.Name = Name
                        TempTotal.Visits += Val(Itm.SubItems(3).Text)
                        TempTotal.Calls += Val(Itm.SubItems(2).Text)
                    End If
                Next
                'Get total sales and transactions for each name
                For Each Itm As ListViewItem In lv.Items
                    If Itm.SubItems(1).Text = Name Then
                        'Average sales will be Sales/NumSales
                        TempTotal.Sales += Val(Itm.SubItems(9).Text)
                        TempTotal.NumSales += 1
                    End If
                Next
                Activity.Item(CheckActivityForName(Name)) = TempTotal
            Next
        End Sub
        Public Function CheckActivityForName(ByVal Name As String) As Integer
            'false is the default
            CheckActivityForName = -1
            For Each Total As Totals In Activity
                'true returns the index of the item
                If Name = Total.Name Then
                    CheckActivityForName = Activity.IndexOf(Total)
                    Return CheckActivityForName
                End If
            Next
        End Function
    End Class

I will check on it I'm very sorry to trouble you but thank you so much :)

Your welcome hope it works lol

I got this error

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

and it highlight the this code

Activity.Item(CheckActivityForName(Name)) = TempTotal

on line 69.

Did you click the button twice? Sorry it's getting late for me and I didn't program a reset for the variables involved. i'll make a patch and see if it helps.

No. i only click it once and it shows an error.

Here put these 2 lines in the button_click routine just before the GetTotals statement:

        Activity.Clear()
        AcctMgrs.Clear()
commented: You deserve more, but here...have a thumb up. +6

Still got same error

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        AcctMgrs.Clear()
        Activity.Clear()
        GetTotals()
        For Each Total As Totals In Activity
            ListView2.Items.Add(Total.Name, Total.Name, 0)
            ListView2.Items(Total.Name).SubItems.Add(Str(Total.Calls))
            ListView2.Items(Total.Name).SubItems.Add(Str(Total.Visits))
            ListView2.Items(Total.Name).SubItems.Add(Str(Total.Sales / Total.NumSales))
        Next
    End Sub

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

 Activity.Item(CheckActivityForName(Name)) = TempTotal

DUH!!! it really is getting late. Those 2 lines I gave you should be in there but they weren't the problem. When I was changing the subitem indexes to match what you need I missed one. Line 48 should read AcctMgrs.Add(Itm.SubItems(1).Text)
There that should fix it.

maybe it Name cannot found?

Yeahh its work :) i missed that one... XD
Thank you so much now i can proceed to the next part haha..
have a Good Night Sleep thank you :)

what do you think why i have this kind of bug?
Hhmmmm.... Error

I'm sure that there are no same data in the itm.subitems(2).text in lvAccountManager..

Hmmm I'm not sure. I've made some test listviews and put in some data to test the code, and that never showed up. I saw a couple things that might make it a little more efficient and modified the code, again lol. Perhaps this will help.

Public Class TotalListviewColuimn
    Structure Totals
        Dim Name As String
        Dim NumSales As Integer
        Dim Sales As Long
        Dim Visits As Integer
        Dim Calls As Integer
    End Structure
    Dim Activity As New List(Of Totals)
    Dim AcctMgrs As New List(Of String)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Activity.Clear()
        AcctMgrs.Clear()
        ListView3.Items.Clear()
        GetTotals()
        For Each Total As Totals In Activity
            ListView3.Items.Add(Total.Name, Total.Name, 0)
            ListView3.Items(Total.Name).SubItems.Add(Str(Total.Calls))
            ListView3.Items(Total.Name).SubItems.Add(Str(Total.Visits))
            ListView3.Items(Total.Name).SubItems.Add(Format(Total.Sales / Total.NumSales, "fixed"))
        Next

    End Sub
    Public Sub GetTotals()
        Dim TempTotal As Totals
        Dim Indx As Long
        With TempTotal
            .Name = ""
            .Visits = 0
            .Sales = 0
            .Calls = 0
            .NumSales = 0
        End With
        'Load each name fro lvAccountManager into the AcctMgrs list
        For Each Itm As ListViewItem In Me.lvAccountManager.Items
            'Check if the name already exists.  
            'CheckName returns -1 if false, otherwise we ignore it.
            'We only want 1 instance of each name in Activity, 
            'as it will hold the totals
            Indx = CheckActivityForName(Itm.SubItems(1).Text)
            If Indx = -1 Then
                AcctMgrs.Add(Itm.SubItems(1).Text)
            End If
        Next
        For Each Name As String In AcctMgrs
            'Get total visits and calls for each item we put into Activity
            For Each Itm As ListViewItem In ListView1.Items
                If Itm.SubItems(1).Text = Name Then
                    TempTotal.Name = Name
                    TempTotal.Visits += Val(Itm.SubItems(3).Text)
                    TempTotal.Calls += Val(Itm.SubItems(2).Text)
                End If
            Next
            'Get total sales and transactions for each item
            For Each Itm As ListViewItem In lv.Items
                If Itm.Text = Name Then
                    'Average sales will be Sales/NumSales
                    TempTotal.Sales += Val(Itm.SubItems(9).Text)
                    TempTotal.NumSales += 1
                End If
            Next
            Activity.Add(TempTotal)
        Next
    End Sub
    Public Function CheckActivityForName(ByVal Name As String) As Integer
        'false is the default
        CheckActivityForName = -1
        For Each ExistingName As String In AcctMgrs
            'true returns the index of the item
            If Name = ExistingName Then
                CheckActivityForName = AcctMgrs.IndexOf(ExistingName)
                Return CheckActivityForName
            End If
        Next
    End Function
End Class

oops just remembered forgot to change back one of the subitems. Line 57 shoud be If Itm.SubItems(1).Text = Name Then

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.