VBA retrieve multiple items from one cell

Heather515

New Member
Joined
Sep 11, 2021
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hi,

I need help to retrieve multiple items from one cell. Data and results are shown below. I want to get multiple keywords from one cell. I created a dictionary. However, it can only retrieve one item from the cell. For example, in B2 and B6, it only shows 300. Thanks.

Function VBA(Rng As Range)

Dim dict As Dictionary

Set dict = New Dictionary

dict("Apple") = "300"
dict("Orange") = "400"
dict("Cat") = "500"
dict("Banana") = "600"


VBA = ""
For Each Key In dict.Keys
If InStr(Rng.Text, Key) Then
VBA = dict(Key)
Exit For
End If
Next

End Function
 

Attachments

  • Data.png
    Data.png
    14.8 KB · Views: 13

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
How about

VBA Code:
Function jec(cell As String) As Variant
 Dim ar, ar2, c00, i
 ar = Array("Apple", "Orange", "Cat", "Banana")
 ar2 = Array(300, 400, 500, 600)
 
 For i = 0 To UBound(ar)
   If InStr(cell, ar(i)) Then c00 = c00 & " " & ar2(i)
 Next
 jec = Mid(c00, 2)
End Function
 
Upvote 0
Hi, any reason for not using native functions?

If you have no objections, here's an option you can try (probably doesn't require committing with Ctl+Shift+Enter with your 365 subscription - but might do.)

Book1
ABCDEF
1TextFormula ResultSearch forReturn
2notenotenoteApplenoteOrangeCatNote300 400 500Apple300
3noteCatBanananotenotenote500 600Orange400
4notenotenote Cat500
5OrangenotenoteCatNote400 500Banana600
6BanananotenoteApple300 600
Sheet1
Cell Formulas
RangeFormula
B2:B6B2=TEXTJOIN(" ",TRUE,IF(ISNUMBER(FIND($E$2:$E$5,A2)),$F$2:$F$5,""))
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0
Solution
How about

VBA Code:
Function jec(cell As String) As Variant
 Dim ar, ar2, c00, i
 ar = Array("Apple", "Orange", "Cat", "Banana")
 ar2 = Array(300, 400, 500, 600)
 
 For i = 0 To UBound(ar)
   If InStr(cell, ar(i)) Then c00 = c00 & " " & ar2(i)
 Next
 jec = Mid(c00, 2)
End Function
Thanks. It works!
 
Upvote 0
Hi, any reason for not using native functions?

If you have no objections, here's an option you can try (probably doesn't require committing with Ctl+Shift+Enter with your 365 subscription - but might do.)

Book1
ABCDEF
1TextFormula ResultSearch forReturn
2notenotenoteApplenoteOrangeCatNote300 400 500Apple300
3noteCatBanananotenotenote500 600Orange400
4notenotenote Cat500
5OrangenotenoteCatNote400 500Banana600
6BanananotenoteApple300 600
Sheet1
Cell Formulas
RangeFormula
B2:B6B2=TEXTJOIN(" ",TRUE,IF(ISNUMBER(FIND($E$2:$E$5,A2)),$F$2:$F$5,""))
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
Good idea! It works perfectly!
 
Upvote 0
I am guessing that is not your real data, but just a slight word of warning. If it is possible that one word could also be part of another word, you could get unusual results.
Here is an example with the worksheet formula suggestion where 4 numbers are returned where I suspect you would only want "300 400 500" returned?
A similar situation could also arise with the UDF suggestion.

If we knew what your real data was like (see XL2BB) then it may be possible to avoid such situations if indeed they are possible.

Heather515.xlsm
ABCDEF
1TextFormula ResultSearch forReturn
2notenotenoteApplenoteOrangeCaterpillarNote300 400 500 700Apple300
3Orange400
4Caterpillar500
5Banana600
6Cat700
Sheet1
Cell Formulas
RangeFormula
B2B2=TEXTJOIN(" ",TRUE,IF(ISNUMBER(FIND($E$2:$E$6,A2)),$F$2:$F$6,""))
 
Upvote 0
I am guessing that is not your real data, but just a slight word of warning. If it is possible that one word could also be part of another word, you could get unusual results.
Here is an example with the worksheet formula suggestion where 4 numbers are returned where I suspect you would only want "300 400 500" returned?
A similar situation could also arise with the UDF suggestion.

If we knew what your real data was like (see XL2BB) then it may be possible to avoid such situations if indeed they are possible.

Heather515.xlsm
ABCDEF
1TextFormula ResultSearch forReturn
2notenotenoteApplenoteOrangeCaterpillarNote300 400 500 700Apple300
3Orange400
4Caterpillar500
5Banana600
6Cat700
Sheet1
Cell Formulas
RangeFormula
B2B2=TEXTJOIN(" ",TRUE,IF(ISNUMBER(FIND($E$2:$E$6,A2)),$F$2:$F$6,""))

Thanks for the warning. I encountered the problems you mentioned. I figured out a workaround that replaces some words so each keyword I want to return value with is unique and doesn't include part of any other words.
 
Upvote 0

Forum statistics

Threads
1,214,645
Messages
6,120,711
Members
448,984
Latest member
foxpro

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