I have an excel cell A1 having cell content {"Q15","R15","S15"}.
How can I use A1 cell in array formula in excel?
I have used this formula:
=SUM(COUNTIFS('sheet1'!$D$2:$D$838,"<>NA",'sheet1'!$E$2:$E$838,{"Q15","R15","S15"}))
the above formula works fine.
but when replaced with A1, it gives 0. And formula becomes
=SUM(COUNTIFS('sheet1'!$D$2:$D$838,"<>NA",'sheet1'!$E$2:$E$838,A1))
How can I use A1 so that the resulting formula contains the actual array?
The problem you have here is that Excel is reading {"Q15","R15","S15"}
from A1
as a String ( ="{""Q15"",""R15"",""S15""}"
), not as an array.
You can get around this using MID
and TRIM
to artificially split the string via a SUMPRODUCT
=SUMPRODUCT(COUNTIFS('sheet1'!$D$2:$D$838, "<>NA", 'sheet1'!$E$2:$E$838, TRIM(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1, """,""", REPT(" ", 99)), "{""", ""), """}", ""), 99*ROW(A1:INDEX(A:A, 1+LEN(A1)-LEN(SUBSTITUTE(A1, ",", ""))))-98, 60))))
The "magic" bit here is the rather convoluted line below
TRIM(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,""",""",REPT(" ",99)),"{""",""),"""}",""),99*Row(A1:INDEX(A:A,1+Len(A1)-Len(SUBSTITUTE(A1,",","")))-98,60))
Turns {"Q15","R15","S15"}
into Q15
, R15
and S15
, separated by 99 spaces. It then counts how many commas are in A1
, and (that many times) takes 60 characters (starting at position 1 the first time, then position 100 the second time, then position 199, et cetera) and trims the spaces - this gives an array {"Q15","R15","S15"}
So, as Tim Williams says in comments, you might be better off using VBA. A basic User Defined Function to split the array like this would work:
Public Function SplitArray(Text As String) As Variant
'If we error, show #NA!
SplitArray = CVErr(xlErrNA)
On Error GoTo FunctionErred
'Split the array
If Left(Text, 1) = "{" And Right(Text, 1) = "}" Then
SplitArray = Split(Replace(Mid(Text, 2, Len(Text) - 2), """", ""), ",")
Else
SplitArray = Text
End If
FunctionErred:
End Function
Giving
=SUM(COUNTIFS('sheet1'!$D$2:$D$838,"<>NA",'sheet1'!$E$2:$E$838,SplitArray(A1)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.