Assign Value to each word in a string

HockeyDiablo

Board Regular
Joined
Apr 1, 2016
Messages
182
I am looking to assign a value to words, then total the value. I am only coming up with solutions that add one value and not all of them.

Cat=1
Dog=2
Bird=3
Lizard=4

Example:

[TABLE="class: grid, align: left"]
<tbody>[TR]
[TD="align: center"][/TD]
[TD="align: center"]A "String"[/TD]
[TD="align: center"]B "Solution"[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: center"]Cat Dog Bird[/TD]
[TD="align: center"]6[/TD]
[/TR]
[TR]
[TD="align: center"]2[/TD]
[TD="align: center"]Dog[/TD]
[TD="align: center"]2[/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD="align: center"]Bird Lizard[/TD]
[TD="align: center"]7[/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD="align: center"]Cat Dog[/TD]
[TD="align: center"]3[/TD]
[/TR]
</tbody>[/TABLE]









Thank you
 
Last edited:
Darn, even the improved version has some issues. Consider the string "Cat Cat". I modified the modified version to:

{=SUM(((LEN(" "&SUBSTITUTE(A2," "," ")&" ")-LEN(SUBSTITUTE(" "&SUBSTITUTE(A2," "," ")&" "," "&$D$2:$D$5&" ","")))/LEN(" "&$D$2:$D$5&" ")*$E$2:$E$5))}

and it seems to handle that case. Even so, I think JoeMo's UDF in #6 is probably the best choice. No worries about non-numeric data in E2:E5 for example.
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Darn, even the improved version has some issues. Consider the string "Cat Cat.
What do you see that was wrong with the "improved" formula I posted in Message #9 with respect to "Cat Cat"? The value of Cat was 1 and "Cat Cat" evaluates to 2, 1 for each appearance of the word... isn't that what you would expect it to do?
 
Upvote 0
Right, I expect to see 2, but when I put the #9 formula in B2 I get 1:

ABCDEF
String#9 formuladifferent versionWordValue#11 formula
Cat CatCat
DogDog
Bird LizardBird
Cat DogLizard

<tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]

[TD="align: right"]1[/TD]
[TD="align: right"]2[/TD]

[TD="align: right"]1[/TD]
[TD="align: right"]2[/TD]

[TD="align: center"]3[/TD]

[TD="align: right"]2[/TD]
[TD="align: right"]2[/TD]

[TD="align: right"]2[/TD]
[TD="align: right"]2[/TD]

[TD="align: center"]4[/TD]

[TD="align: right"]7[/TD]
[TD="align: right"]7[/TD]

[TD="align: right"]3[/TD]
[TD="align: right"]7[/TD]

[TD="align: center"]5[/TD]

[TD="align: right"]3[/TD]
[TD="align: right"]3[/TD]

[TD="align: right"]4[/TD]
[TD="align: right"]3[/TD]

</tbody>
Sheet1

Array Formulas
Cell

<tbody>
[TH="align: left"]Formula[/TH]

[TH="bgcolor: #DAE7F5"]B2[/TH]
[TD="align: left"]{=SUM( ((LEN(" "&A2&" ")-LEN(SUBSTITUTE(" "&A2&" "," "&$D$2:$D$5&" ","")))/LEN(" "&$D$2:$D$5&" ")*$E$2:$E$5) )}

<tbody>

[TH="bgcolor: #DAE7F5"]C2[/TH]
[TD="align: left"]{=SUM( ((LEN(" "&A2&" ")-LEN(SUBSTITUTE(" "&A2&" ",$D$2:$D$5&" ","")))/LEN($D$2:$D$5&" ")*$E$2:$E$5) )}[/TD]

[TH="bgcolor: #DAE7F5"]F2[/TH]
[TD="align: left"]{=SUM( ((LEN(" "&SUBSTITUTE(A2," "," ")&" ")-LEN(SUBSTITUTE(" "&SUBSTITUTE(A2," "," ")&" "," "&$D$2:$D$5&" ","")))/LEN(" "&$D$2:$D$5&" ")*$E$2:$E$5) )}[/TD]

</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself [/TD]

</tbody>




If I only append a space to the end of the D2:D5 words, I get the formula in C2 and I get a result of 2. And F2 shows the formula I proposed in post #11.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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