Some extra notes for this Thread:
http://www.eileenslounge.com/viewtopic.php?f=30&t=38460
Hans Solution http://www.eileenslounge.com/viewtop...297266#p297266
This is a nice solution which I totally misread, or rather in my ignorance, I did not understand.
The main point I missed is…
The solution assumes that the final solution actually has a 26 element 1 dimensional array, and the weight numbers in that array are sorted in alphabetical order, so that the first element represents the weight for “A” and the last Element represents the weight for “Z”, etc.
( So the array Letters() is redundant, and only the Weights() array is needed )
Hans has kindly set me straight and explained where I was going wrong. The final working version of his solution is
How is that working:Code:Sub Testit() MsgBox prompt:=Weight("ZAC") End Sub ' https://eileenslounge.com/viewtopic.php?f=30&t=38460&sid=4295ec4560088f42492ca29590271a87 Public Function Weight(S As String) As Long ' http://www.eileenslounge.com/viewtopic.php?p=297266#p297266 Dim Weights() As Variant ' Letters() As Variant, Dim i As Long ' Letters = Array("A", "B", "C", ..., "Z") ' Weights = Array(1, 5, 3, ..., 2) ' A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z Let Weights() = Array(1, 5, 3, 1, 4, 3, 2, 1, 6, 4, 5, 3, 2, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 2) ' Watch : - : Weights() : : Variant/Variant(0 to 25) : Module1.Weight For i = 1 To Len(S) Let Weight = Weight + Weights(Asc(Mid(S, i, 1)) - 65) Next i End Function
We are looping through each character, then doing something clever to get the running total. The clever bit is getting the array element
To demonstrate that working consider a couple of examples for the case of a word having an A and a Z in it
A has the Ascii Code number of 65. So we end up referring to Weights(65-65) = Weights(0) , which is the first element typically in a 1 dimensional array that starts at indicia 0
Z has the Ascii Code number of 90. So we end up referring to Weights(90-65) = Weights(25) , which is the last element in a 1 dimensional array of 26 elements that starts at indicia 0
In order for the function to get correct results in the case of lower case letters, then one way to do it, ( assuming you have the correct Weights() array you want for lower case letters), you would need to change the 65 to 97
Code:Sub Testit() Debug.Print Tab(4); "ASCII"; Tab(12); "Weight" Debug.Print Tab(4); "Code" Call Weight("ZAC") Debug.Print Call WeightLowerCase("zac") End Sub ' https://eileenslounge.com/viewtopic.php?f=30&t=38460&sid=4295ec4560088f42492ca29590271a87 Public Function Weight(S As String) As Long ' http://www.eileenslounge.com/viewtopic.php?p=297266#p297266 Dim Weights() As Variant ' Letters() As Variant, Dim i As Long ' A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z Let Weights() = Array(1, 5, 3, 1, 4, 3, 2, 1, 6, 4, 5, 3, 2, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 2) ' Watch : - : Weights() : : Variant/Variant(0 to 25) : Module1.Weight For i = 1 To Len(S) Let Weight = Weight + Weights(Asc(Mid(S, i, 1)) - 65) Debug.Print Mid(S, i, 1); Tab(4); Asc(Mid(S, i, 1)); Tab(8); Asc(Mid(S, i, 1)) - 65; Tab(12); Weights(Asc(Mid(S, i, 1)) - 65) Next i End Function Public Function WeightLowerCase(S As String) As Long ' http://www.eileenslounge.com/viewtopic.php?p=297266#p297266 Dim Weights() As Variant ' Letters() As Variant, Dim i As Long ' a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z Let Weights() = Array(1, 5, 3, 1, 4, 3, 2, 1, 6, 4, 5, 3, 2, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 2) ' For i = 1 To Len(S) Let WeightLowerCase = WeightLowerCase + Weights(Asc(Mid(S, i, 1)) - 97) Debug.Print Mid(S, i, 1) & vbTab & Asc(Mid(S, i, 1)) & vbTab & Asc(Mid(S, i, 1)) - 97 & vbTab & Weights(Asc(Mid(S, i, 1)) - 97) Next i End Function
Here is the Debug.Print output from the last demo coding
Code:ASCII Weight Code Z 90 25 2 A 65 0 1 C 67 2 3 z 122 25 2 a 97 0 1 c 99 2 3
Bookmarks