Solved

vba - search a cell with text for a particular value

Posted on 2010-11-08
7
254 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unable to open excel in 2016 is slow 4 21
Struggling to Lock and grey out cells using VBA. Help please! 8 27
Add a range in an Excel graph 5 36
excel help 4 20
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now