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:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Here's a UDF you can try.
Sheet2

*AB
StringSum

<colgroup><col style="font-weight:bold; *******3**** "><col style="*******82px;"><col style="*******64px;"></colgroup><tbody>
[TD="bgcolor: #cacaca, align: center"]1[/TD]

[TD="bgcolor: #cacaca, align: center"]2[/TD]
[TD="align: center"]Cat Dog Bird[/TD]
[TD="align: center"]6[/TD]

[TD="bgcolor: #cacaca, align: center"]3[/TD]
[TD="align: center"]Dog[/TD]
[TD="align: center"]2[/TD]

[TD="bgcolor: #cacaca, align: center"]4[/TD]
[TD="align: center"]Bird Lizard[/TD]
[TD="align: center"]7[/TD]

[TD="bgcolor: #cacaca, align: center"]5[/TD]
[TD="align: center"]Cat Dog[/TD]
[TD="align: center"]3[/TD]

</tbody>

Spreadsheet Formulas
CellFormula
B2=sumthings(A2)

<tbody>
</tbody>

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

Code:
Function SumThings(S As String) As Double
Dim Creatures As Variant, Vals As Variant, V As Variant, i As Long, j As Long
Creatures = Array("Cat", "Dog", "Bird", "Lizard")
Vals = Array(1, 2, 3, 4)
V = Split(S, " ")
For i = 0 To UBound(V)
    For j = 0 To UBound(Creatures)
        If V(i) = Creatures(j) Then
            SumThings = SumThings + Vals(j)
        End If
    Next j
Next i
End Function
NOTE: This is case sensitive as posted
 
Last edited:
Upvote 0
Here's a UDF you can try.
Sheet2

*AB
StringSum

<tbody>
[TD="bgcolor: #cacaca, align: center"]1[/TD]

[TD="bgcolor: #cacaca, align: center"]2[/TD]
[TD="align: center"]Cat Dog Bird[/TD]
[TD="align: center"]6[/TD]

[TD="bgcolor: #cacaca, align: center"]3[/TD]
[TD="align: center"]Dog[/TD]
[TD="align: center"]2[/TD]

[TD="bgcolor: #cacaca, align: center"]4[/TD]
[TD="align: center"]Bird Lizard[/TD]
[TD="align: center"]7[/TD]

[TD="bgcolor: #cacaca, align: center"]5[/TD]
[TD="align: center"]Cat Dog[/TD]
[TD="align: center"]3[/TD]

</tbody>

Spreadsheet Formulas
CellFormula
B2=sumthings(A2)

<tbody>
</tbody>

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

Code:
Function SumThings(S As String) As Double
Dim Creatures As Variant, Vals As Variant, V As Variant, i As Long, j As Long
Creatures = Array("Cat", "Dog", "Bird", "Lizard")
Vals = Array(1, 2, 3, 4)
V = Split(S, " ")
For i = 0 To UBound(V)
    For j = 0 To UBound(Creatures)
        If V(i) = Creatures(j) Then
            SumThings = SumThings + Vals(j)
        End If
    Next j
Next i
End Function
NOTE: This is case sensitive as posted

Exactly what I need, thank you kindly
 
Upvote 0
Here's a UDF you can try.
Sheet2

*AB
StringSum

<tbody>
[TD="bgcolor: #cacaca, align: center"]1[/TD]

[TD="bgcolor: #cacaca, align: center"]2[/TD]
[TD="align: center"]Cat Dog Bird[/TD]
[TD="align: center"]6[/TD]

[TD="bgcolor: #cacaca, align: center"]3[/TD]
[TD="align: center"]Dog[/TD]
[TD="align: center"]2[/TD]

[TD="bgcolor: #cacaca, align: center"]4[/TD]
[TD="align: center"]Bird Lizard[/TD]
[TD="align: center"]7[/TD]

[TD="bgcolor: #cacaca, align: center"]5[/TD]
[TD="align: center"]Cat Dog[/TD]
[TD="align: center"]3[/TD]

</tbody>

Spreadsheet Formulas
CellFormula
B2=sumthings(A2)

<tbody>
</tbody>

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

Code:
Function SumThings(S As String) As Double
Dim Creatures As Variant, Vals As Variant, V As Variant, i As Long, j As Long
Creatures = Array("Cat", "Dog", "Bird", "Lizard")
Vals = Array(1, 2, 3, 4)
V = Split(S, " ")
For i = 0 To UBound(V)
    For j = 0 To UBound(Creatures)
        If V(i) = Creatures(j) Then
            SumThings = SumThings + Vals(j)
        End If
    Next j
Next i
End Function
NOTE: This is case sensitive as posted

Just curious on how you could make this not case sensitive? Easy?
 
Upvote 0
I see its too late and my solution is not so variable and sofisticated :-) its not a functioin....you can change your range depending in which cells you have your animals:-) but you dont need it anymore. Anyway....

Code:
Sub test()

Dim cat As Integer
Dim dog As Integer
Dim bird As Integer
Dim lizard As Integer
Dim cell As Range
Dim rng As Range
Dim word1 As String
Dim word2 As String
Dim word3 As String
Dim word4 As String

word1 = "cat"
word2 = "dog"
word3 = "bird"
word4 = "lizard"

Set rng = [b9:b12]


cat = 1
dog = 2
bird = 3
lizard = 4

For Each cell In rng.Cells
    
    If Not cell.Find(word1) Is Nothing Then
      cell.Offset(0, 1).Value = cell.Offset(0, 1).Value + cat
    End If
    
    If Not cell.Find(word2) Is Nothing Then
      cell.Offset(0, 1).Value = cell.Offset(0, 1).Value + dog
    End If
    
    
    If Not cell.Find(word3) Is Nothing Then
      cell.Offset(0, 1).Value = cell.Offset(0, 1).Value + bird
    End If
    
    If Not cell.Find(word4) Is Nothing Then
      cell.Offset(0, 1).Value = cell.Offset(0, 1).Value + lizard
    End If
 

Next



End Sub
 
Last edited:
Upvote 0
Just curious on how you could make this not case sensitive? Easy?
Easy. This revision is NOT case sensitive:
Code:
Function SumThings(S As String) As Double
Dim Creatures As Variant, Vals As Variant, V As Variant, i As Long, j As Long
Creatures = Array("Cat", "Dog", "Bird", "Lizard")
Vals = Array(1, 2, 3, 4)
V = Split(S, " ")
For i = 0 To UBound(V)
    For j = 0 To UBound(Creatures)
        If LCase(V(i)) = LCase(Creatures(j)) Then
            SumThings = SumThings + Vals(j)
        End If
    Next j
Next i
End Function
 
Upvote 0
You can do it with formulas too:

ABCDE
StringSolutionWordValue
Cat Dog BirdCat
DogDog
Bird LizardBird
Cat DogLizard

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

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

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

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

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

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

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

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

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

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

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

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

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

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

</tbody>
Sheet8

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>

</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>

Enter the formula in B2, change the ranges to match your sheet, then confirm with Control+Shift+Enter. Then drag it down the column.


To make it non case-sensitive, change the formula to:
=SUM(((LEN(A2)-LEN(SUBSTITUTE(LOWER(A2),LOWER($D$2:$D$5),"")))/LEN($D$2:$D$5)*$E$2:$E$5))
confirmed with Control+Shift+Enter.

There some issues with this. If you have a word in your cell that partially matches your word list, it will count it. Doggie will be counted as dog, and catastrophe will be counted as cat. You'll have to look at your actual word list to see if this will matter. You'll probably want to stick with Joe's UDF since you already have that working.
 
Last edited:
Upvote 0
Your formula is not robust though...

Catholic Dogma Birdie
However, if we assume only spaces surrounding the words (that is, no parentheses, commas, periods, etc.), then this modification to your formula appears to work more robustly...

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

Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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