Solved

vba - search a cell with text for a particular value

Posted on 2010-11-08
7
256 Views
Last Modified: 2012-05-10
I have a cell filled with text...lengthy...I need to search that text for a particular key word like "a is for apple" and "b is for bat"....if those phases are found in that cell filled with 30 or more sentences then phase_found = "y" else phrase_found = "n'
0
Comment
Question by:GlobaLevel
7 Comments
 
LVL 39

Expert Comment

by:nutsch
ID: 34088017
if instr(cl.value,"a is for apple")>0 then phrase_found="y" else phrase_found="n" '

Thomas
0
 
LVL 12

Expert Comment

by:sdwalker
ID: 34088058
In vba, use this ...

Sub testString()

  If InStr(1, Range("A1").Value, y, vbTextCompare) > 0 Then
    Range("B1").Value = "y"
  Else
    Range("B1").Value = "n"
  End If
  
End Sub

Open in new window

0
 
LVL 24

Expert Comment

by:broomee9
ID: 34088070
You actually don't need VBA (you may still want it though).  Here's a formula approach that uses column B to enter your keyword:

=IF(AND(ISNUMBER(FIND(B2,A2,1)),LEN(A2)-LEN(SUBSTITUTE(A2, CHAR(10), ""))>30),"yes","no")

If you don't want column B as a helper, then you can add the phrase to the formula by replacing the word "test" here:

=IF(AND(ISNUMBER(FIND("test",A2,1)),LEN(A2)-LEN(SUBSTITUTE(A2, CHAR(10), ""))>30),"yes","no")

Book1.xls
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 24

Expert Comment

by:broomee9
ID: 34088100
>>are found in that cell filled with 30 or more sentences

I took this to meant 30 more breaks in the cell (ie. Alt + Enter).  That's what CHAR(10) will find.  If you mean sentences, and you actually want to count periods, then use this formula instead:

=IF(AND(ISNUMBER(FIND("test",A2,1)),LEN(A2)-LEN(SUBSTITUTE(A2, ".", ""))>30),"yes","no")
0
 
LVL 33

Expert Comment

by:Norie
ID: 34095917
Do you just want to check for one phrase?
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 34095972
Multiple
0
 
LVL 39

Accepted Solution

by:
nutsch earned 500 total points
ID: 34096095
On the formula side, you can put all the phrases you want to check for in a range and use the following formula, for instance with your phrases in H1:H4 and your text cell in A1


=SUMPRODUCT(ISERROR(FIND(H1:H4,A1))*1)=0

it will return TRUE if all phrases are found, false if they aren't.

in a sub, look at the attached function that matches all phrases defined in the array

Thomas

Function phrase_found(strText As String) As String
Dim arrPhrases(1 To 26) As String, i As Long

arrPhrases(1) = "a is for apple"
arrPhrases(2) = "b is for bat"
'...
arrPhrases(26) = "z is for zebra"
  
For i = 1 To 26
    If InStr(strText, arrPhrases(i)) = 0 Then GoTo NotAllFound:
Next i

phrase_found = "y"
Exit Function

NotAllFound:
phrase_found = "n"
End Function

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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