Results 1 to 14 of 14

Thread: [RESOLVED] Count unique numbers in a listview column?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Resolved [RESOLVED] Count unique numbers in a listview column?

    Hello experts
    I need to to count unique numbers in a listview column.
    In other words it is like we substitute by Group By when dealing with database.
    So I have a column like that:

    10
    20
    10
    30
    30
    10
    40
    20

    So I have:
    10 - 20 - 30 and 40

    I want to get occurance = 4
    thank you
    Last edited by Mustaphi; Oct 31st, 2018 at 07:13 AM.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Count some occurances in a listview column?

    Use a collection with
    Code:
    On Error Resume Next
    For i=1 to ListViewRowCount 
       MyCollection.Add ListViewValue, ListViewValue 'Note: Adding ListViewValue as the Value for the Collection-Item but also as the Key to the Item
    Next
    On Error Goto 0
    Debug.Print MyCollection.Count
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: Count some occurances in a listview column?

    Thanks sir
    I'm getting Objet required here
    Code:
    Debug.Print MyCollection.Count

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: Count some occurances in a listview column?

    this how I'm doing
    Code:
    For i = 1 To ListView1.ListItems.Count
       MyCollection.Add ListView1.ListItems(i).SubItems(1), ListView1.ListItems(i).SubItems(1) 'Note: Adding ListViewValue as the Value for the Collection-Item but also as the Key to the Item
    Next
    On Error GoTo 0
    Debug.Print MyCollection.Count
    Last edited by Mustaphi; Oct 31st, 2018 at 09:31 AM.

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Count some occurances in a listview column?

    Did you declare MyCollection? My guess is that the .Add is also throwing an error but the resume next is hiding it. All most likely because you didn't declare your variable.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: Count some occurances in a listview column?

    techgnome
    thanks for your interest
    Please can you telle me how should I declare it.
    Code:
    Dim MyCollection As String
    didn't solve the issue

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Count some occurances in a listview column?

    Quote Originally Posted by Mustaphi View Post
    techgnome
    thanks for your interest
    Please can you telle me how should I declare it.
    Code:
    Dim MyCollection As String
    didn't solve the issue


    Quote Originally Posted by Zvoni View Post
    Use a collection
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: Count unique numbers in a listview column?

    techgnome
    would provide some help please because no idea about using collection

  9. #9
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Count unique numbers in a listview column?

    If you don't what know collections are then better read the help first.
    Press F1 and search for "collections"

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: Count unique numbers in a listview column?

    Quote Originally Posted by Arnoutdv View Post
    If you don't what know collections are then better read the help first.
    Press F1 and search for "collections"
    I already declared it as collection but did not work
    Code:
    Dim MyCollection As Collection

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Count unique numbers in a listview column?

    Right... this isn't your first trip to the rodeo... think about it... it's a collection object. You can't use an object until you create it... how do you create a new instance of an object? Again... how do you create a NEW instance of an object.... think about it... a NEW object... hint hint hint....


    -tg

    ps - yes, I'm making you use your brain here....
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: Count unique numbers in a listview column?

    Code:
    Dim MyCollection As Collection
    Set MyCollection = New Collection
    For i = 1 To ListView1.ListItems.Count
    MyCollection.Add ListView1.ListItems(i).SubItems(1), ListView1.ListItems(i).SubItems(1) 'Note: Adding ListViewValue as the Value for the Collection-Item but also as the Key to the ItemNext
    Next
    On Error GoTo 0
    Debug.Print MyCollection.Count
    Error
    the key is already associated with an element of the collection

  13. #13
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Count unique numbers in a listview column?

    Where is the "On Error Resume Next"?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: Count unique numbers in a listview column?

    Zvoni
    thank you very much
    solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width