?
Solved

count specific word in multiline textbox

Posted on 2008-10-19
5
Medium Priority
?
563 Views
Last Modified: 2012-05-05
I just need to count how many times a specific word shows up in a multiline textbox. It only occurs once per line. The code only counts the first line and stops and can't figure out how to make it CR without locking up my prog. I am not against using different code, so if it is not possible with this one something else would be fine. I am open to suggestions. There could be anywhere from 1 to 100+  lines in the textbox, it changes depending upon input.
Dim Total As Integer
   Dim Index1 As Integer
   Dim OIndex
 
   Index1 = 1
   While Index1 >= OIndex
       Index1 = InStr(Index1, Form.TextBox1.text, "SEARCHWORD") + 1
       If Index1 > OIndex Then
         Total = Total + 1
         OIndex = Index1
       End If
   Wend

Open in new window

0
Comment
Question by:guacolizard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 22753820
Dim Count As Long
Count = (Length(Form.TextBox1.text) - Length(Replace(Form.TextBox1.text, "SEARCHWORD", ""))) / Length("SEARCHWORD")

Kevin
0
 

Author Comment

by:guacolizard
ID: 22753846
I am getting a sub or function not defined error with "length"...what should I dim it as?
0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 1000 total points
ID: 22753877
Dim Count As Long
Count = (Len(Form.TextBox1.text) - Len(Replace(Form.TextBox1.text, "SEARCHWORD", ""))) / Len("SEARCHWORD")

Kevin
0
 

Author Closing Comment

by:guacolizard
ID: 31507657
Thank you, works awesome!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22754123
Here is an example of doing it like you originally envisioned:
    Private Function GetCount(ByVal strToSearch As String, ByVal searchFor As String) As Integer
        Dim startAt As Integer
        Dim foundAt As Integer
 
        startAt = 1
        Do
            foundAt = InStr(startAt, strToSearch, searchFor)
            If foundAt <> 0 Then
                GetCount = GetCount + 1
                startAt = foundAt + Len(searchFor)
            End If
        Loop While foundAt <> 0
    End Function

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question