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:

A "String"B "Solution"
1Cat Dog Bird6
2Dog2
3Bird Lizard7
4Cat Dog3

<tbody>
</tbody>









Thank you
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Here's a UDF you can try.
Sheet2

*AB
1StringSum
2Cat Dog Bird6
3Dog2
4Bird Lizard7
5Cat Dog3

<colgroup><col style="font-weight:bold; *******3**** "><col style="*******82px;"><col style="*******64px;"></colgroup><tbody>
</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
1StringSum
2Cat Dog Bird6
3Dog2
4Bird Lizard7
5Cat Dog3

<tbody>
</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
1StringSum
2Cat Dog Bird6
3Dog2
4Bird Lizard7
5Cat Dog3

<tbody>
</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
1StringSolutionWordValue
2Cat Dog Bird6Cat1
3Dog2Dog2
4Bird Lizard7Bird3
5Cat Dog3Lizard4

<tbody>
</tbody>
Sheet8

Array Formulas
CellFormula
B2{=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

<tbody>
</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,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

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