calculate array

keda duck

Board Regular
Joined
Jul 9, 2023
Messages
54
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
Platform
  1. Windows
This is a problem I encountered in my work. I want to use formula to solve this problem

Each cell is two numbers separated by character "/", and the number may contain Decimal separator.
The cells may be empty.
B3 and B2 are compared and assigned scores, C3 and C2 are compared and assigned scores...
Finally, the sum of the assigned scores for each row and the second row is calculated and written in column W for each row.

Scoring rules:
2: Two cells are the same and neither is empty
1: A certain number in cell B3 is the same as two numbers in cell B2
0: At least one of the two cells is empty, or the two numbers in cell B3 are different from the two numbers in cell B2

Taking the sixth row as an example.
B6 is empty, so its score is 0.
the first number of c6 is 9, and the second number of c4 is 13, these numbers are not existed in c2, so its score is 0.
The first number of d6 is 9, and the second number of b2 is 12, 12 is existed in b2 but 9 is not existed in, so its score is 1.
……
the first number of j6 is 8, and the second number of j6 is 11, 8 and 11 are both in j2, so its score is 2.
……
and so on.

When I calculate the score for each cell, I sum the scores for each row.

calculate.xlsx
ABCDEFGHIJKLMNOPQRSTUVW
1no.data 1data 2data 3data 4data 5data 6data 7data 8data 9data 10data 11data 12data 13data 14data 15data 16data 17data 18data 19data 20data 21
2116/168/811/129/925.2/30.212/1510/1130/308/1116/17.314/1411/1514/1410/1118/2717/2416/209/912/2111/1224/24
3215/1711/1212/1211/1319/28.213/1511/1230/318/813/18.313/1415/1712/1314/1417/2319/2316/207/715/1710/1122/22
4315/1610/118/1011/1322/28.215/167/1130/328/1114/1512/15.211/1510/1411/1419/2324/2515/196/915/1512/1221/22
5417/179/108/1010/1120/27.212/1410/1129/31.28/811/1413/1513/1811/1110/1418/2017/2214/177/915/158/1122/23
659/139/1211/1125.2/26.213/1311/1229/308/1112/1513/15.216/1612/1310/1219/1916/2014/186/915/169/1424/24
7615/1610/128/812/1319.2/24.215/1611/1129/32.28/812/1313/1315/1714/1511/1421/2323/2414/199/914/2011/1223/25
8715/168/118/1112/1322/24.215/1611/1131.2/328/813/1413/15.215/1610/1511/1218/2322/2417/197/914/1510/1222/23
9814/189/1112/139/1425.2/28.216/1611/1232/32.28/915/1613/1417/1710/1210/1419/2116/2314/157/813/1412/1224/25
10915/178/118/1111/1122.2/28.214/1510/1230/318/817/1713.2/14.211/1511/1411.3/1218/1920/2317/187/913/2012/1224/25
111015/178/88/1311/1219/28.210/1029/308/815/1713/14.211/1111/1210/11.318/2019/2017/189/913/1412/1223/25
121117/189/1111/1122.2/28.215/1512/1229/318/1013.2/1415/1513/1411/1219/2114/177/914/2011/1221/24
131214/1610/119/129/1119/26.213/1410/1329/31.28/815/1513/1415/1611/1418/1923/2414/169/913/1610/1024/25
141315/1612/139/1119/27.213/1411/1230/31.28/815/1513/14.215/1611/1410/1218/1919/2314/187/913/1310/1223/25
151415/1510/1112/1310/1019/27.213/1411/1229/31.28/1015/1513/1411/1511/1312/1218/1919/2314/169/913/1610/1223/24
161515/1610/1210/119/1028.2/29.213/1411/1229/298/913/1513/1311/1713/1510/1121/2219/2316/196/714/1811/1223/24
171616/189/128/1111/1219/2113/1512/1332/33.211/1111/1413/14.211/1513/1610/1117/1819/2018/197/816/2310/1223/24
181715/158/98/1311/1223.2/28.213/1311/1330/309/1114/1714.2/14.215/1613/1410/1118/1924/2414/189/1013/1310/1219/20
191816/178/1111/1112/1220/25.213/169/1230/31.210/1112/1512.2/15.215/1612/1410/1018/2019/2016/187/713/1412/1320/23
201914/159/119/139/1019/30.213/1412/1331.2/33.28/1015/1511/1613/1410/1219/1919/2414/177/913/1610/1224/25
212015/168/1012/1210/1126.2/27.213/1310/1129/308/813/1513/1411/1511/1110/1218/1823/2316/189/913/1610/1223/24
222110/138/1012/1219/30.214/1512/1329/308/815/1515/16.211/1612/1311/1418/1919/2316/199/915/1610/1222/23
Sheet1 (2)
 
Is this it?
1689157863256.png


VBA Code:
Sub Score()
Dim Wb As Workbook
Dim Ws As Worksheet
Dim ValStr As String
Dim ValSep As String
Dim ValSepLoc As Integer
Dim Val1 As Single
Dim Val2 As Single
Dim Val1R2 As Single
Dim Val2R2 As Single
Dim Rowloop As Long
Dim Colloop As Long

'Scoring rules:
'2: Two cells are the same and neither is empty
'1: A certain number in cell Bn is the same as two numbers in cell B2
'0: At least one of the two cells is empty, or the two numbers in cell Bn are different from the two numbers in cell B2

Set Wb = ThisWorkbook
Set Ws = Wb.Worksheets("Sheet1")
ValSep = "/"
Val1 = 0
Val2 = 0
Val1R2 = 0
Val2R2 = 0

For Colloop = 2 To 22
    For Rowloop = 2 To 22
        ValStr = Ws.Cells(Rowloop, Colloop).Value
        ValSepLoc = InStr(1, ValStr, ValSep)
        If ValSepLoc > 1 Then ' cell is blank or no value left of separator
            Val1 = Left(ValStr, ValSepLoc - 1)
        Else
            Val1 = 0
            Ws.Cells(Rowloop + 24, Colloop).Value = 0
        End If
        If ValSepLoc < Len(ValStr) Then ' no value right of separator
            Val2 = Right(ValStr, Len(ValStr) - ValSepLoc)
        Else
            Val2 = 0
        End If
        If Rowloop = 2 Then
            Val1R2 = Val1
            Val2R2 = Val2
        Else
            If Val1 = Val1R2 And Val2 = Val2R2 Then
                Ws.Cells(Rowloop + 24, Colloop).Value = 2 'values are the same
            ElseIf (Val1 <> Val1R2 And Val2 = Val2R2) Or (Val1 = Val1R2 And Val2 <> Val2R2) Then
                Ws.Cells(Rowloop + 24, Colloop).Value = 1 '1 value are different
            Else
                Ws.Cells(Rowloop + 24, Colloop).Value = 0 'No match
            End If
        End If
    Next Rowloop
Next Colloop
End Sub
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Is this it?
View attachment 95099

VBA Code:
Sub Score()
Dim Wb As Workbook
Dim Ws As Worksheet
Dim ValStr As String
Dim ValSep As String
Dim ValSepLoc As Integer
Dim Val1 As Single
Dim Val2 As Single
Dim Val1R2 As Single
Dim Val2R2 As Single
Dim Rowloop As Long
Dim Colloop As Long

'Scoring rules:
'2: Two cells are the same and neither is empty
'1: A certain number in cell Bn is the same as two numbers in cell B2
'0: At least one of the two cells is empty, or the two numbers in cell Bn are different from the two numbers in cell B2

Set Wb = ThisWorkbook
Set Ws = Wb.Worksheets("Sheet1")
ValSep = "/"
Val1 = 0
Val2 = 0
Val1R2 = 0
Val2R2 = 0

For Colloop = 2 To 22
    For Rowloop = 2 To 22
        ValStr = Ws.Cells(Rowloop, Colloop).Value
        ValSepLoc = InStr(1, ValStr, ValSep)
        If ValSepLoc > 1 Then ' cell is blank or no value left of separator
            Val1 = Left(ValStr, ValSepLoc - 1)
        Else
            Val1 = 0
            Ws.Cells(Rowloop + 24, Colloop).Value = 0
        End If
        If ValSepLoc < Len(ValStr) Then ' no value right of separator
            Val2 = Right(ValStr, Len(ValStr) - ValSepLoc)
        Else
            Val2 = 0
        End If
        If Rowloop = 2 Then
            Val1R2 = Val1
            Val2R2 = Val2
        Else
            If Val1 = Val1R2 And Val2 = Val2R2 Then
                Ws.Cells(Rowloop + 24, Colloop).Value = 2 'values are the same
            ElseIf (Val1 <> Val1R2 And Val2 = Val2R2) Or (Val1 = Val1R2 And Val2 <> Val2R2) Then
                Ws.Cells(Rowloop + 24, Colloop).Value = 1 '1 value are different
            Else
                Ws.Cells(Rowloop + 24, Colloop).Value = 0 'No match
            End If
        End If
    Next Rowloop
Next Colloop
End Sub
No, the results of this code are error. such as h27, the first number of h3 is 13, and the second number of h3 is 15, 15 is existed in h2 but 13 is not existed in, so h27 is 1.
If the results of this code can be summed for each row and output in column W, it would be even better.
 
Upvote 0
Removed as incorrect.
 
Last edited:
Upvote 0
How about
Excel Formula:
=SUM(MAP($B$2:$V$2,B3:V3,LAMBDA(a,b,SUM(--ISNUMBER(FIND("/"&TEXTSPLIT(b,"/",,1)&"/","/"&a&"/"))))))
Sorry, the results of this formula are error. Maybe I didn't explain it clearly.
Such as row3, the first and the second number of h3 is 12, 12 is existed in h2 but the first number of h2 is not existed in h3, so its score is 1.
The first and the second number of j3 is 8, 8 is existed in j2 but the second number of j2 is not existed in j3, so its score is 1.
The results of your formula are both 2 scores.
 
Upvote 0
I have been playing with this, does the answer column below give the correct answer for the data i have been using?

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQ
1no.data 1data 2data 3data 4data 5data 6data 7data 8
2116/1625.2/30.230/3014/1418/2717/2416/2024/24161625.230.2303014141827172416202424Answer
3215/1719/28.230/3113/1417/2319/2316/2022/2215171928.23031131417231923162022220000100100001104
4315/1622/28.230/3212/15.219/2324/2515/1921/2215162228.230321215.219232425151921220100100000000002
5417/1720/27.229/31.213/1518/2017/2214/1722/2317172027.22931.2131518201722141722230000000010100002
6516/1625.2/26.229/3013/15.219/1916/2014/1824/24161625.226.229301315.219191620141824241110010000000015
7615/1619.2/24.229/32.213/1321/2323/2414/1923/25151619.224.22932.2131321232324141923250100000000010002
8715/1622/24.231.2/3213/15.218/2322/2417/1922/2315162224.231.2321315.218232224171922230100000010010003
9814/1825.2/28.232/32.213/1419/2116/2314/1524/25141825.228.23232.2131419211623141524250010000100000013
10915/1722.2/28.230/3113.2/14.218/1920/2317/1824/25151722.228.2303113.214.218192023171824250000100010000013
111015/1719/28.229/3013/14.218/2019/2017/1823/2515171928.229301314.218201920171823250000010010000002
121117/1822.2/28.229/3113.2/1419/2114/1721/24171822.228.2293113.214192100141721240000000100000001
131214/1619/26.229/31.213/1418/1923/2414/1624/2514161926.22931.2131418192324141624250100000110010015
141315/1619/27.230/31.213/14.218/1919/2314/1823/2515161927.23031.21314.218191923141823250100100010000003
151415/1519/27.229/31.213/1418/1919/2314/1623/2415151927.22931.2131418191923141623240000000110000002
161515/1628.2/29.229/2913/1321/2219/2316/1923/24151628.229.22929131321221923161923240100000000001002
171616/1819/2132/33.213/14.217/1819/2018/1923/24161819213233.21314.217181920181923241000000000000001
181715/1523.2/28.230/3014.2/14.218/1924/2414/1819/20151523.228.2303014.214.218192424141819200000110010010004
191816/1720/25.230/31.212.2/15.218/2019/2016/1820/2316172025.23031.212.215.218201920161820231000100010001004
201914/1519/30.231.2/33.219/1919/2414/1724/2514151930.231.233.20019191924141724250001000000010013
212015/1626.2/27.229/3013/1418/1823/2316/1823/24151626.227.22930131418182323161823240100010110001005
222119/30.229/3015/16.218/1919/2316/1922/23001930.229301516.218191923161922230001010010001004
Sheet1
Cell Formulas
RangeFormula
K2:Z22K2=LET(rng,BYROW(MAP(B2:I22,LAMBDA(x,IF(x = "","0/0",x))),LAMBDA(x,TEXTJOIN("/",FALSE,x))), c, MAX(BYROW(rng,LAMBDA(x,LEN(x)-LEN(SUBSTITUTE(x,"/",""))+1))), xml, "<t><s>" & SUBSTITUTE(rng,"/","</s><s>") & "</s></t>", IFERROR(FILTERXML(xml,"//s[" & SEQUENCE(,c) & "]"),""))
AB3:AP22AB3=IF(K3:K22=K2,1,0)
AQ3:AQ22AQ3=SUM(AB3:AP3)
Dynamic array formulas.
 
Upvote 0
How about
Excel Formula:
=SUM(MAP($B$2:$V$2,B3:V3,LAMBDA(a,b,LET(x,TEXTSPLIT(b,"/"),y,SUM(--ISNUMBER(FIND("/"&TEXTSPLIT(b,"/",,1)&"/","/"&a&"/"))),IF(INDEX(x,1)=INDEX(x,2),IF(b=a,y,y/2),y)))))
 
Upvote 0
A slightly simplified version
Excel Formula:
=SUM(MAP($B$2:$V$2,B3:V3,LAMBDA(a,b,LET(x,TEXTSPLIT(b,"/"),y,SUM(--ISNUMBER(FIND("/"&TEXTSPLIT(b,"/",,1)&"/","/"&a&"/"))),IF((INDEX(x,1)=INDEX(x,2))*(b<>a),y/2,y)))))
 
Upvote 0
I have been playing with this, does the answer column below give the correct answer for the data i have been using?

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQ
1no.data 1data 2data 3data 4data 5data 6data 7data 8
2116/1625.2/30.230/3014/1418/2717/2416/2024/24161625.230.2303014141827172416202424Answer
3215/1719/28.230/3113/1417/2319/2316/2022/2215171928.23031131417231923162022220000100100001104
4315/1622/28.230/3212/15.219/2324/2515/1921/2215162228.230321215.219232425151921220100100000000002
5417/1720/27.229/31.213/1518/2017/2214/1722/2317172027.22931.2131518201722141722230000000010100002
6516/1625.2/26.229/3013/15.219/1916/2014/1824/24161625.226.229301315.219191620141824241110010000000015
7615/1619.2/24.229/32.213/1321/2323/2414/1923/25151619.224.22932.2131321232324141923250100000000010002
8715/1622/24.231.2/3213/15.218/2322/2417/1922/2315162224.231.2321315.218232224171922230100000010010003
9814/1825.2/28.232/32.213/1419/2116/2314/1524/25141825.228.23232.2131419211623141524250010000100000013
10915/1722.2/28.230/3113.2/14.218/1920/2317/1824/25151722.228.2303113.214.218192023171824250000100010000013
111015/1719/28.229/3013/14.218/2019/2017/1823/2515171928.229301314.218201920171823250000010010000002
121117/1822.2/28.229/3113.2/1419/2114/1721/24171822.228.2293113.214192100141721240000000100000001
131214/1619/26.229/31.213/1418/1923/2414/1624/2514161926.22931.2131418192324141624250100000110010015
141315/1619/27.230/31.213/14.218/1919/2314/1823/2515161927.23031.21314.218191923141823250100100010000003
151415/1519/27.229/31.213/1418/1919/2314/1623/2415151927.22931.2131418191923141623240000000110000002
161515/1628.2/29.229/2913/1321/2219/2316/1923/24151628.229.22929131321221923161923240100000000001002
171616/1819/2132/33.213/14.217/1819/2018/1923/24161819213233.21314.217181920181923241000000000000001
181715/1523.2/28.230/3014.2/14.218/1924/2414/1819/20151523.228.2303014.214.218192424141819200000110010010004
191816/1720/25.230/31.212.2/15.218/2019/2016/1820/2316172025.23031.212.215.218201920161820231000100010001004
201914/1519/30.231.2/33.219/1919/2414/1724/2514151930.231.233.20019191924141724250001000000010013
212015/1626.2/27.229/3013/1418/1823/2316/1823/24151626.227.22930131418182323161823240100010110001005
222119/30.229/3015/16.218/1919/2316/1922/23001930.229301516.218191923161922230001010010001004
Sheet1
Cell Formulas
RangeFormula
K2:Z22K2=LET(rng,BYROW(MAP(B2:I22,LAMBDA(x,IF(x = "","0/0",x))),LAMBDA(x,TEXTJOIN("/",FALSE,x))), c, MAX(BYROW(rng,LAMBDA(x,LEN(x)-LEN(SUBSTITUTE(x,"/",""))+1))), xml, "<t><s>" & SUBSTITUTE(rng,"/","</s><s>") & "</s></t>", IFERROR(FILTERXML(xml,"//s[" & SEQUENCE(,c) & "]"),""))
AB3:AP22AB3=IF(K3:K22=K2,1,0)
AQ3:AQ22AQ3=SUM(AB3:AP3)
Dynamic array formulas.

When I saw formula =IF(K3:K22=K2,1,0), I suspected it is wrong. This may not necessarily correspond to one column, but may require two columns to be compared to each other.
 
Upvote 0
A slightly simplified version
Excel Formula:
=SUM(MAP($B$2:$V$2,B3:V3,LAMBDA(a,b,LET(x,TEXTSPLIT(b,"/"),y,SUM(--ISNUMBER(FIND("/"&TEXTSPLIT(b,"/",,1)&"/","/"&a&"/"))),IF((INDEX(x,1)=INDEX(x,2))*(b<>a),y/2,y)))))
That is a lot closer, but still a few issues as I see it.
If a cell is empty, an error will be reported in column W. I added iferror to your formula to solve this problem.
Excel Formula:
=SUM(IFERROR(MAP($B$2:$V$2,B3:V3,LAMBDA(a,b,LET(x,TEXTSPLIT(b,"/"),y,SUM(--ISNUMBER(FIND("/"&TEXTSPLIT(b,"/",,1)&"/","/"&a&"/"))),IF((INDEX(x,1)=INDEX(x,2))*(b<>a),y/2,y)))),))
Do you have any other solutions to solve this problem?
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,206
Members
453,022
Latest member
RobertV1609

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