Results 1 to 13 of 13

Thread: [Resolved]Counting specific words

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    301

    Resolved [Resolved]Counting specific words

    Hello all,

    I have a textbox with for example this text:

    89655 154654 9898 Water

    465465 497987 1469 Fire

    01 4987 1878 Water

    45465 11123 4/* Water

    89655 154654 9898 Water+ Fire

    465465 497987 1469 Fire

    01 4987 1878 Water
    Now I want to count how many times the word Fire, and how many times the work Water are there.

    In this example:
    5 Water
    3 Fire


    Thanks
    Last edited by Account; Oct 11th, 2007 at 12:52 AM.

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Counting specific words


  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Counting specific words

    This function may help!
    Code:
    Option Explicit
    
    Function InStrOccur(String1 As String, String2 As String, _
                        Optional Compare As VbCompareMethod = vbBinaryCompare) As Long
       Dim p1 As Long
       Dim p2 As Long
       Dim n As Long
    
       If String2 = "" Then Exit Function
       p1 = 1
       p2 = InStr(p1, String1, String2, Compare)
       Do While p2 > 0
          n = n + 1
          p1 = p2 + Len(String2)
          p2 = InStr(p1, String1, String2, Compare)
       Loop
       InStrOccur = n
    End Function
    
    Sub Test()
       Dim s As String
       s = "89655 154654 9898 Water" & vbCrLf & _
           "465465 497987 1469 Fire" & vbCrLf & _
           "01 4987 1878 Water" & vbCrLf & _
           "45465 11123 4/* Water" & vbCrLf & _
           "89655 154654 9898 Water+ Fire" & vbCrLf & _
           "465465 497987 1469 Fire" & vbCrLf & _
           "01 4987 1878 Water"
       Debug.Print InStrOccur(s, "Water")
       Debug.Print InStrOccur(s, "water")
       Debug.Print InStrOccur(s, "water", vbTextCompare)
       Debug.Print InStrOccur(s, "Fire")
       Debug.Print InStrOccur(s, "")
       Debug.Print InStrOccur("", "Water")
    End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    301

    Re: Counting specific words

    Wow this helps!


    Really thanks all!

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Counting specific words

    If you like my post, rate it.
    If you feel Ok with solution, mark the thread resolved.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    301

    Re: Counting specific words

    ok.

    Now I have a other problem.

    How to count more then 1 word.

    So not only Water, but also Water + Fire

    i'm using anh's method for the static textfile [changes nothing]
    and I am using Bruce Fox for the dynamic textfile [values changes]

    So with this code;

    vb Code:
    1. Private Sub Command1_Click()
    2.    
    3.       Dim iTotal%, iPos%
    4.          Dim strText$
    5.  
    6.           strText = Text1.Text
    7.  
    8.           iPos = 1
    9.  
    10.           Do While iPos <= Len(strText)
    11.  
    12.               If InStr(iPos, UCase(strText), "WORD") > 0 Then
    13.  
    14.                   iTotal = iTotal + 1
    15.  
    16.                   iPos = InStr(iPos, UCase(strText), "WORD") + Len("WORD")
    17.  
    18.               Else
    19.  
    20.                   Exit Do
    21.  
    22.               End If
    23.  
    24.           Loop
    25.  
    26.           MsgBox "Total occurences - " & iTotal
    27.  
    28.       End Sub

    How to count the words fire, and water, and do them plus each other.

    For exmaple:
    5 times water
    3 Fire:
    8 in total

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Counting specific words

    Oh! Do you know how to add 2 numbers together?
    Code:
    Private Sub Command1_Click()
       Dim n as Long
       
       n = InStrOccur(Text1.Text, "Water") + InStrOccur(Text1.Text, "Fire")
    
       MsgBox n
    
    End Sub

  8. #8
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Counting specific words

    Spilt would do a good job here also
    Code:
    Option Explicit
    Dim strToCheck As String
    
    Private Sub Command1_Click()
    Dim strMess As String
    Dim intWater As Integer
    Dim intFire As Integer
    
        intWater = WordCount(strToCheck, "water")
        intFire = WordCount(strToCheck, "fire")
        
        strMess = "Water " & intWater & " times" & vbCrLf
        strMess = strMess & "Fire " & intFire & " times" & vbCrLf
        strMess = strMess & "Total " & intWater + intFire & " matches"
        
        MsgBox strMess
    End Sub
    
    Private Sub Form_Load()
    Dim strLines(6) As String
    
        strLines(0) = "89655 154654 9898 Water"
        strLines(1) = "465465 497987 1469 Fire"
        strLines(2) = "01 4987 1878 Water"
        strLines(3) = "45465 11123 4/* Water"
        strLines(4) = "89655 154654 9898 Water+ Fire"
        strLines(5) = "465465 497987 1469 Fire"
        strLines(6) = "01 4987 1878 Water"
        
        strToCheck = Join(strLines, vbCrLf)
    End Sub
    
    
    Private Function WordCount(ByVal SearchText As String, ByVal SearchWord As String, Optional blnCaseSensitive As Boolean = False) As Integer
    Dim strArr() As String
    
        If blnCaseSensitive Then
            strArr = Split(SearchText, SearchWord)
        Else
            strArr = Split(LCase(SearchText), LCase(SearchWord))
        End If
        WordCount = UBound(strArr)
    End Function

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    301

    Re: Counting specific words

    Thanks for the help, but as I mentioned: the textfile is dynamic.

    Code:
       1.
          Private Sub Form_Load()
    
              Dim stgSearch      As String
    
              Dim stgWordCount() As String
    
              stgSearch = "I wrote these words, I put my word on it!"
    
              stgWordCount = Split(stgSearch, "word", -1)
    
              Call MsgBox(UBound(stgWordCount))
    
          End Sub

    I am trying to search for more combinations:
    Code:
    Private Sub Command1_Click()
              Dim stgSearch      As String
    
              Dim stgWordCount1() As String
              Dim stgWordCount2() As String
              Dim stgWordCount3() As String
              Dim stgWordCount4() As String
              Dim stgWordCount5() As String
              Dim stgWordCount6() As String
    
              stgSearch = Text1.Text
    
    stgWordCount1 = Split(stgSearch, "water", -1)
     stgWordCount2 = Split(stgSearch, "Water", -1)
      stgWordCount3 = Split(stgSearch, "WATER", -1)
      
       stgWordCount4 = Split(stgSearch, "fire", -1)
        stgWordCount5 = Split(stgSearch, "Fire", -1)
         stgWordCount6 = Split(stgSearch, "FIRE", -1)
     
    End Sub
    But how to count the stgWordCount1 + stgWordCount2 + stgWordCount3 etc and show that in a msgbox?

    Thnx

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Counting specific words

    How about
    Code:
    Msgbox stgWordCount1.Ubound + stgWordCount2.Ubound + stgWordCount3.Ubound

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    301

    Re: Counting specific words

    Compile error:
    invalid qualifier

    With as highlighted:
    stgWordCount1

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Counting specific words

    It seems Hack was thinking VB.Net instead of Classic VB, it should be like this:

    Msgbox UBound(stgWordCount1) + ...

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    301

    Re: Counting specific words

    That did the job!

    Thank to all of you!

    Reputation added , Topic 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