|
-
Oct 6th, 2007, 04:54 AM
#1
Thread Starter
Hyperactive Member
[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.
-
Oct 6th, 2007, 04:59 AM
#2
Re: Counting specific words
-
Oct 6th, 2007, 06:04 AM
#3
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
-
Oct 6th, 2007, 07:09 AM
#4
Thread Starter
Hyperactive Member
Re: Counting specific words
Wow this helps!
Really thanks all!
-
Oct 6th, 2007, 08:17 AM
#5
Re: Counting specific words
If you like my post, rate it.
If you feel Ok with solution, mark the thread resolved.
-
Oct 6th, 2007, 02:31 PM
#6
Thread Starter
Hyperactive Member
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:
Private Sub Command1_Click()
Dim iTotal%, iPos%
Dim strText$
strText = Text1.Text
iPos = 1
Do While iPos <= Len(strText)
If InStr(iPos, UCase(strText), "WORD") > 0 Then
iTotal = iTotal + 1
iPos = InStr(iPos, UCase(strText), "WORD") + Len("WORD")
Else
Exit Do
End If
Loop
MsgBox "Total occurences - " & iTotal
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
-
Oct 6th, 2007, 09:05 PM
#7
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
-
Oct 7th, 2007, 07:11 AM
#8
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
-
Oct 10th, 2007, 04:26 AM
#9
Thread Starter
Hyperactive Member
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
-
Oct 10th, 2007, 07:39 AM
#10
Re: Counting specific words
How about
Code:
Msgbox stgWordCount1.Ubound + stgWordCount2.Ubound + stgWordCount3.Ubound
-
Oct 10th, 2007, 10:04 AM
#11
Thread Starter
Hyperactive Member
Re: Counting specific words
Compile error:
invalid qualifier
With as highlighted:
stgWordCount1
-
Oct 10th, 2007, 11:56 AM
#12
Re: Counting specific words
It seems Hack was thinking VB.Net instead of Classic VB, it should be like this:
Msgbox UBound(stgWordCount1) + ...
-
Oct 10th, 2007, 02:01 PM
#13
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|