Solved

Excel formula/macro - How can I identify a 3 character word & then pull the entire sentence to an adjacent cell?

Posted on 2014-09-17
11
397 Views
Last Modified: 2014-09-18
Hi.  I have a cell with a lot of technology names.

I need to only pull sentences with EHR or EMR within the sentence, and then pull the entire sentence (between the commas) into an adjacent cell.

For example:

,Allscripts Enterprise EHR, Cerner, Cerner Millennium, Certify HealthDock,

I want to pull only "Allscripts Enterprise EHR" in another cell.

Thanks!
0
Comment
Question by:Gilbert L.
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 6

Expert Comment

by:johnb25
ID: 40329047
Hi,

If your starting sentence is in A1 in Sheet1, then this will do it. It will put each sub-section that has the letters you are searching in the next cell to the right

John

Function FindPhrase()

    Dim WB As Workbook
    Dim WS As Worksheet
    Dim InputValue As String
    Dim Sentence() As String
    Dim A As Integer
    Dim B As Integer
    
    Set WB = ThisWorkbook
    Set WS = WB.Worksheets("Sheet1")
    B = 2
    
    InputValue = WS.Cells(1, 1).Value
    
    Sentence = Split(InputValue, ",")
    
    For A = LBound(Sentence) To UBound(Sentence)
    
        If InStr(Sentence(A), "EHR") > 0 Then
            WS.Cells(1, B).Value = Sentence(A)
            B = B + 1
        ElseIf InStr(Sentence(A), "EMR") > 0 Then
            WS.Cells(1, B).Value = Sentence(A)
            B = B + 1
        End If
    
    Next
    
End Function

Open in new window

0
 

Author Comment

by:Gilbert L.
ID: 40329076
Sorry for my ignorance, but I tried to create a macro from this and it does not work with the Sub.  How can I engage the code?

Thanks.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40329077
John's code isn't complete, but the right idea.

If you want a user-defined function to return the first substring with either EHR or EMR in it, then use this code:
Function FindPhrase(InputValue As String) As String
    Dim Sentence() As String
    Dim A As Integer
    
    Sentence = Split(InputValue, ",")
    For A = LBound(Sentence) To UBound(Sentence)
        If InStr(Sentence(A), "EHR") > 0 Then
           FindPhrase = Trim(Sentence(A))
        ElseIf InStr(Sentence(A), "EMR") > 0 Then
           FindPhrase = Trim(Sentence(A))
        End If
    Next A
End Function

Open in new window

0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40329080
What do you want to have happen if any of the substrings occur more than once in a cell?  For example:

here is some text with EHR, then another string, then another with EMR.

By the way, I forgot to mention in the previous post, you use the UDF in Excel like a regular function.  Just type in an adjacent cell
=FindPhrase(cell_reference)
and it should return the last-occurring substring.

Regards,
-Glenn
0
 

Author Comment

by:Gilbert L.
ID: 40329096
Also, I have 456 rows to look at.  When I tried it with Alt F11, Run - It only did A1.  Can it go all the way down?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Gilbert L.
ID: 40329109
Glen, your code works but some of the records may have both EMR and EHR and the code seems to only pick one or the other
0
 
LVL 6

Assisted Solution

by:johnb25
johnb25 earned 200 total points
ID: 40329154
I have amended the code to allow you select the range of cells that have long sentences.
I will loop through and do what the first code did for each one.

Glenn,
The B=B+1 in the original code was used to put each segment out in the next column.

John

Function FindPhrase()

    Dim WB As Workbook
    Dim WS As Worksheet
    Dim InputRange As Range
    Dim InputValue As String
    Dim Sentence() As String
    Dim A As Integer
    Dim B As Integer
    
    Set WB = ThisWorkbook
    Set WS = WB.Worksheets("Sheet1")
    
    Set InputRange = Application.InputBox(prompt:="Select range of sentences", Type:=8)
    
    For Each c In InputRange
        InputValue = c.Value
        Sentence = Split(InputValue, ",")
    For A = LBound(Sentence) To UBound(Sentence)
        B = 1
        If InStr(Sentence(A), "EHR") > 0 Then
            c.Offset(0, B).Value = Sentence(A)
            B = B + 1
        ElseIf InStr(Sentence(A), "EMR") > 0 Then
            c.Offset(0, B).Value = Sentence(A)
            B = B + 1
        End If
    
    Next
    Next
End Function

Open in new window

0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 300 total points
ID: 40329182
Unfortunately, john's code is set up as a function and only runs on one cell.

If all your data (sentences) are in column A, starting in cell A1, this code will loop through all of them and insert the substrings in adjacent column(s), starting in column B:
Sub ListPhrases()
    Dim rng As Range
    Dim cl As Object
    Dim arrPhrases() As String
    Dim x, c As Integer
    
    'Change "A1" to the first cell where sentences occur to test
    Set rng = Range("A1", Range("A1").End(xlDown))
    
    For Each cl In rng
        arrPhrases = Split(cl.Value, ",")
        c = 1
        For x = 0 To UBound(arrPhrases)
            If InStr(arrPhrases(x), "EHR") > 0 Then
                cl.Offset(0, c).Value = Trim(arrPhrases(x))
                c = c + 1
            ElseIf InStr(arrPhrases(x), "EMR") > 0 Then
                cl.Offset(0, c).Value = Trim(arrPhrases(x))
                c = c + 1
            End If
        Next x
    Next cl
End Sub

Open in new window


Regards,
-Glenn
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 40330104
Gilbert:

The contributions so far are checking for any existence of the three-character literal strings; not explicitly just those three characters distinct from any other text.

This may well address your requirements & allow you to retrieve the correct results as intended.

However, for example:

",Allscripts Enterprise EHR, Cerner, Cerner Millennium, Certify HealthDock,"

"EHR" would be detected & "Allscripts Enterprise EHR" would be removed (as intended).

BUT... what if the search text looked like this:

"FUEHRER,Allscripts Enterprise EHR, Cerner, Cerner Millennium, Certify HealthDock,"

"FUEHRER" would then be identified as containing "EHR" & this is perhaps an incorrect result.

Please could you clarify if the search terms ("EHR" & "EMR") need to be distinct (discrete) values or whether the inclusion of the three characters (in order) within a larger collection of characters would also be considered a 'match', or not?

Thank you.
0
 

Author Comment

by:Gilbert L.
ID: 40330491
Thanks John/Glenn!  This has been immensely helpful!  What I finally did was take the macro and created one for EMR and one for EHR, ran them separately and then concatenated them into a column for EMR and a column for EHR.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 40330610
You're welcome.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

With the internet and the ease of information transference, many professional jobs can be done anywhere today.  Why should it make a difference whether an x-ray is read in India or the United States as long as the radiologist is qualified?   Outso…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 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

17 Experts available now in Live!

Get 1:1 Help Now