Find specific words in another sheet, return lookup

joand

Active Member
Joined
Sep 18, 2003
Messages
267
I have 2 worksheets ("database", and "source").

The sheet "database" contains two columns ("A" and "B") where all disease names are listed in column "A" and their treatment (or pharmaceutical drugs) are listed in Column B.

The other worksheet "source" contains one column ("A") where "A1:A15" contains rows of paragraphs. I want to search each item (or row) or disease name in the sheet "source" and if the disease is found, returns the value of pharmaceutical drug that will be used to treat the disease in cell B1, separated by semi-colon for each succeeding drug/treatment.

For example, in sheet "database" A1:=Cholera, B1:=oral rehydration therapy, A2:=Diphtheria, B2:=Metronidazole. I want to search the diseases Cholera and Diphtheria in the "source" worksheet. The source sheet is in paragraph form. If it finds both diseases, then B1 value is "oral rehydration therapy: Metronidazole". The disease list (words to search) in column A number around 100+.

Anyone has an idea?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
joand,

Can we see some of the paragraphs in worksheet source?


What version of Excel are you using?

You will generally get much more help (and faster) in this forum if you can post your small samples (what you have and what you expect to achieve) directly in the forum.

To attach screenshots, see below in my Signature block: Post a screen shot with one of these:
 
Upvote 0
Hi joand,

I think that you need a VBA solution to your problem, unless you are happy having the data returned in separate columns...


Excel Workbook
AB
1NameData1
2CatRed
3DogGreen
4HorseBlue
5MouseBlack
6PigWhite
7BullYellow
Database




Excel Workbook
ABCDEF
1PhraseCountData1
2Cat in a hat1Red***
3Hat with a cat inside1Red***
4Dilbert the dog1Green***
5Dog and Cat2RedGreen**
6House with a mouse1Black***
7Angry bull1Yellow***
8House with a mouse with a cat and Dog3RedGreenBlack*
Source


The formula in B2 needs to be copied down.
The formula in C2 needs entering with ctrl shift enter NOT enter.
You can then copy that formula across and down.

I hope that helps you.

Ak
 
Upvote 0
joand,


Sample worksheets:


Excel Workbook
AB
1Choleraoral rehydration therapy
2DiphtheriaMetronidazole
3
database





Excel Workbook
AB
1search the diseases Cholera and Diphtheria
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
source





After the macro:


Excel Workbook
AB
1search the diseases Cholera and Diphtheriaoral rehydration therapy: Metronidazole
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
source





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Option Base 1
Sub GetTreatment()
' hiker95, 06/12/2011
' http://www.mrexcel.com/forum/showthread.php?t=556597
Dim ws As Worksheet, wd As Worksheet
Dim a As Long, aa As Long, H As String
Dim D() As Variant, S() As Variant
Application.ScreenUpdating = False
Set ws = Worksheets("source")
Set wd = Worksheets("database")
D = wd.UsedRange.Value
S = ws.Range("A1:B15").Value
For a = LBound(S) To UBound(S)
  H = ""
  For aa = LBound(D) To UBound(D)
    If InStr(S(a, 1), D(aa, 1)) > 0 Then
      H = H & D(aa, 2) & ": "
    End If
  Next aa
  If H <> "" Then
    If Right(H, 2) = ": " Then H = Left(H, Len(H) - 2)
    S(a, 2) = H
  End If
Next a
ws.Range("A1:B15").Value = S
ws.UsedRange.Columns.AutoFit
ws.Activate
Erase D: Erase S
Application.ScreenUpdating = True
End Sub


Then run the GetTreatment macro.
 
Upvote 0
Hi hiker:

How do I change this macro so that it won't be case sensitive. The problem is when I input "cholera" and "diptheria" in the "source" sheet it does not return the treatment value since it only "knows" Cholera and Diptheria (proper case)
 
Upvote 0
joand,


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub GetTreatmentV3()
' hiker95, 06/14/2011
' http://www.mrexcel.com/forum/showthread.php?t=556597
Dim a As Long, aa As Long, H As String
Dim D() As Variant, S() As Variant
D = Worksheets("database").UsedRange.Value
S = Worksheets("source").Range("A1:B15").Value
For a = LBound(S) To UBound(S)
  H = ""
  For aa = LBound(D) To UBound(D)
    If InStr(LCase(S(a, 1)), LCase(D(aa, 1))) > 0 Then
      H = H & D(aa, 2) & ": "
    End If
  Next aa
  If H <> "" Then
    If Right(H, 2) = ": " Then H = Left(H, Len(H) - 2)
    S(a, 2) = H
  End If
Next a
Worksheets("source").Range("A1:B15").Value = S
Worksheets("source").Columns(2).AutoFit
Erase D: Erase S
End Sub


Then run the GetTreatmentV3 macro.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top