Results 1 to 17 of 17

Thread: counting strings in a listbox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    counting strings in a listbox

    Hi
    I have a listbox that may contain up to 100 items,but many items are of the same format:
    e.g
    1daily25042006
    12daily25042006
    35morning25042006
    13daily25042006
    1morning25042006

    I want to count the total number of items containing the string daily(in the above example there is 3),I want also to count the total number of items containing morning and i'll continue looping through the listbox,if i find items with another string,i'll count the total number of it occurence

    how can i do it?
    thanks

  2. #2
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: counting strings in a listbox

    Try something along these lines
    VB Code:
    1. Dim Daily As Integer, Morning As Integer
    2. Dim i As Integer
    3.  
    4.  For i = 0 To List1.ListCount - 1
    5.   If InStr(1, List1.List(i), "daily") Then
    6.    Daily = Daily + 1
    7.   ElseIf InStr(1, List1.List(i), "morning") Then
    8.    Morning = Morning + 1
    9.   End If
    10.  Next i
    also i would consider having some global variables and each time you add an item then you can check and keep count.

    casey.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Re: counting strings in a listbox

    hI
    well I tried a code like ur code but the problem is that I don't know at the begining what are the items of the listbox,what i want to do is counting the items that contain the same string,ur code works well if I know all the string that are contained in the listbox

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: counting strings in a listbox

    Quote Originally Posted by engnouna
    Hi
    I have a listbox that may contain up to 100 items,but many items are of the same format:
    e.g
    1daily25042006
    12daily25042006
    35morning25042006
    13daily25042006
    1morning25042006

    I want to count the total number of items containing the string daily(in the above example there is 3),I want also to count the total number of items containing morning and i'll continue looping through the listbox,if i find items with another string,i'll count the total number of it occurence

    how can i do it?
    thanks
    Do a search for posts by me that have "SendMessageString" in the text. Using that and LB_FindStringExact you could create a second, hidden, listbox and move the List1 items to List2 if they are not already in List2. When done, List2.ListCount will be the count you want.

  5. #5
    Hyperactive Member Bearnerd's Avatar
    Join Date
    Apr 2006
    Location
    Malaysia
    Posts
    290

    Re: counting strings in a listbox

    Do you mean the listbox may contain not only "daily" and "morning" but also another string which you don't know what?

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: counting strings in a listbox

    Quote Originally Posted by Bearnerd
    Do you mean the listbox may contain not only "daily" and "morning" but also another string which you don't know what?
    Sorry I may have misunderstood your question. Will list1 always contain data that contains "daily" and "morning"?

  7. #7
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: counting strings in a listbox

    im sure this can be done better but it works

    VB Code:
    1. Dim cWords As New Collection
    2. Private Sub Count_Words()
    3.     Dim tmp As String
    4.     Dim tmpK() As String
    5.     For x = 0 To List1.ListCount - 1
    6.         tmp = List1.List(x)
    7.         For i = 1 To Len(tmp)
    8.             If IsNumeric(Mid(tmp, i, 1)) Then
    9.                 tmp = Replace(tmp, Mid(tmp, i, 1), ".")
    10.             End If
    11.         Next
    12.         tmp = Replace(tmp, ".", "")
    13.         If Not AddedWordAlready(tmp) Then
    14.             cWords.Add 1 & "|" & tmp, tmp
    15.         Else
    16.             Dim t As Integer
    17.             tmpK = Split(cWords.Item(tmp), "|")
    18.             t = tmpK(0)
    19.             t = t + 1
    20.             cWords.Remove tmp
    21.             cWords.Add t & "|" & tmpK(1), tmp
    22.         End If
    23.     Next
    24.     For x = 1 To cWords.Count
    25.         Debug.Print cWords.Item(x)
    26.     Next
    27. End Sub
    28. Private Function AddedWordAlready(wrd As String) As Boolean
    29.     Dim test As String
    30.     On Error GoTo Added
    31.     test = cWords.Item(wrd)
    32.     AddedWordAlready = True
    33.     Exit Function
    34. Added:
    35.     AddedWordAlready = False
    36.    
    37. End Function
    38.  
    39. Private Sub Command1_Click()
    40.     Count_Words
    41. End Sub
    42.  
    43. Private Sub Form_Load()
    44.     List1.AddItem "1daily25042006"
    45.     List1.AddItem "12daily25042006"
    46.     List1.AddItem "35morning25042006"
    47.     List1.AddItem "13daily25042006"
    48.     List1.AddItem "1morning25042006"
    49. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  8. #8
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: counting strings in a listbox

    output will have
    count|word
    like this

    3|daily
    2|morning


    and it will work with any set of words..
    removes all numbers from the word.. then adds to a collection...
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Re: counting strings in a listbox

    Hi Static,
    thanks a lot for ur code but I want to display the results in labels rather than in print.debug window.How can I do it?
    thanks

  10. #10
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    266

    Re: counting strings in a listbox

    then make control array of labels and then using for each loop u can put values in labels by creating as many labels at runtime as u need

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Re: counting strings in a listbox

    10x a lot

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Re: counting strings in a listbox

    Hi Static
    I used ur code but I call the sub Private Sub Count_Words() 3 times for 3 lists,I noticed that I need to remove cWords.Item(x) each time i move to another list
    how can i delete cWords.Item(x)

    because if in list1 there are 3 Daily and in list2 there are 2 Daily
    when I display the results it gives me in list 1 3Daily but in list 2, 5 daily

  13. #13
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: counting strings in a listbox

    ok, if u are outputing to the labels in the sub (Count_words) just move the Dim cWord inside of the sub
    Moving it inside the sub will reset it on each call..
    VB Code:
    1. Private Sub Count_Words()
    2.     Dim cWords As New Collection 'Moved inside the sub
    3.     Dim tmp As String
    4.     Dim tmpK() As String
    5.     For x = 0 To List1.ListCount - 1
    6.         tmp = List1.List(x)
    7.         For i = 1 To Len(tmp)
    8.             If IsNumeric(Mid(tmp, i, 1)) Then
    9.                 tmp = Replace(tmp, Mid(tmp, i, 1), ".")
    10.             End If
    11.         Next
    12.         tmp = Replace(tmp, ".", "")
    13.         If Not AddedWordAlready(tmp) Then
    14.             cWords.Add 1 & "|" & tmp, tmp
    15.         Else
    16.             Dim t As Integer
    17.             tmpK = Split(cWords.Item(tmp), "|")
    18.             t = tmpK(0)
    19.             t = t + 1
    20.             cWords.Remove tmp
    21.             cWords.Add t & "|" & tmpK(1), tmp
    22.         End If
    23.     Next
    24.     For x = 1 To cWords.Count
    25.         Debug.Print cWords.Item(x)
    26.     Next
    27. End Sub
    28. '...
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Re: counting strings in a listbox

    thanks Static
    you've been a great help!!!!!!!!!!!!!!!!!

  15. #15
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: counting strings in a listbox

    glad it work.. Im not good with collections LOL
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  16. #16
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: counting strings in a listbox

    if you use a dictionary object (requires reference to Microsoft Scripting Runtime), then that has an Exists function which avoids the need for error catching:

    VB Code:
    1. ' Call it as:
    2. CountWords List1
    3.  
    4. Private Sub CountWords(ByVal lstBox As ListBox)
    5.     Dim N As Long, I As Long, b() As Byte, stemp As String, dict As New Dictionary
    6.        
    7.     For N = 0 To lstBox.ListCount - 1
    8.         b = lstBox.List(N)
    9.         For I = 0 To UBound(b) Step 2
    10.             If b(I) > 47 And b(I) < 58 Then b(I) = 0
    11.         Next I
    12.         stemp = Replace(b, Chr$(0), vbNullString)
    13.         If dict.Exists(stemp) Then
    14.             dict.Item(stemp) = dict.Item(stemp) + 1
    15.         Else
    16.             dict.Add stemp, 1
    17.         End If
    18.     Next N
    19.    
    20.     ' Print Result:
    21.     For N = 0 To dict.Count - 1
    22.         Debug.Print dict.Keys(N), dict.Item(dict.Keys(N))
    23.     Next N
    24. End Sub

  17. #17
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: counting strings in a listbox

    BAHH!! The Dictionary object is what I was thinking!! couldnt remember
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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