我正在尝试在 E 列中创建一个公式,该公式能够根据其他列生成我在此处显示的内容。解释起来很复杂,但我尝试在 Excel 中尽可能清楚地说明我希望公式获取的内容。非常感谢您的帮助!
我会尝试进一步解释,抱歉。所以我想要一个在 E2、E3、E4 等单元格中的公式,它可以生成我在那些单元格中显示的内容。例如,对于苹果,如果有两个实例,总数为 4,每个实例的数量为 2,那么我希望公式从左到右抓取 D 列中的数字数量,然后对于下一个苹果,我希望它能够在抓取前 2 个之后按顺序抓取接下来的 2 个。我对公式的预期结果就是您在 E 列中看到的结果。
回答 Scott 在评论中提出的问题:
是的,您可以依赖 A 列中具有相同值的行作为一组。
一切都是肯定的。
2a. 我认为 B、C 和 G 列在公式中对于分解内容可能是有用或必要的,但如果 A、D 和 F 列足以获得 E 中的内容,则公式可能不需要它们。
2b. 是的,E 中的结果取决于它之前的行。从组中的第一个到最后一个,基于 A 列。例如,如果它是第一个苹果,它会从左到右取前几个数字,它所取的数字数量基于 F 列。因此,下一个苹果在 F 中的数量为 2,然后它会取前两个分配给组中第一个苹果的数字之后的接下来的 2 个数字,如果有超过 2 个实例,则依此类推。
@Rajesh SI 意识到我应该详细说明实例的含义,因为存在误解。实例只是计算 A 列中每个单独组中有多少个实例。因此,您说得对,B 列对于确定 E 列输入内容的公式来说不是必需的。
F 列,即数量,决定了在 E 列中输入多少内容。
答案1
在 E2 中(向下拖动):
=A2&C2&" - "&LEFT(SUBSTITUTE(IFERROR(RIGHT(IFERROR(RIGHT(IFERROR(SUBSTITUTE(SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&",",",","|",IF((COUNTIF($A$2:A2,A2)-1)=0,0,SUM(OFFSET(F2,-1*(COUNTIF($A$2:A2,A2)-1),0,COUNTIF($A$2:A2,A2)-1)))),SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&","),LEN(IFERROR(SUBSTITUTE(SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&",",",","|",IF((COUNTIF($A$2:A2,A2)-1)=0,0,SUM(OFFSET(F2,-1*(COUNTIF($A$2:A2,A2)-1),0,COUNTIF($A$2:A2,A2)-1)))),SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&","))-FIND("|",IFERROR(SUBSTITUTE(SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&",",",","|",IF((COUNTIF($A$2:A2,A2)-1)=0,0,SUM(OFFSET(F2,-1*(COUNTIF($A$2:A2,A2)-1),0,COUNTIF($A$2:A2,A2)-1)))),SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&","))),IFERROR(SUBSTITUTE(SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&",",",","|",IF((COUNTIF($A$2:A2,A2)-1)=0,0,SUM(OFFSET(F2,-1*(COUNTIF($A$2:A2,A2)-1),0,COUNTIF($A$2:A2,A2)-1)))),SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&",")),LEN(IFERROR(SUBSTITUTE(SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&",",",","|",IF((COUNTIF($A$2:A2,A2)-1)=0,0,SUM(OFFSET(F2,-1*(COUNTIF($A$2:A2,A2)-1),0,COUNTIF($A$2:A2,A2)-1)))),SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&","))-FIND("|",IFERROR(SUBSTITUTE(SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&",",",","|",IF((COUNTIF($A$2:A2,A2)-1)=0,0,SUM(OFFSET(F2,-1*(COUNTIF($A$2:A2,A2)-1),0,COUNTIF($A$2:A2,A2)-1)))),SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&","))),IFERROR(SUBSTITUTE(SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&",",",","|",IF((COUNTIF($A$2:A2,A2)-1)=0,0,SUM(OFFSET(F2,-1*(COUNTIF($A$2:A2,A2)-1),0,COUNTIF($A$2:A2,A2)-1)))),SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&",")),",","]",F2),FIND("]",SUBSTITUTE(IFERROR(RIGHT(IFERROR(SUBSTITUTE(SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&",",",","|",IF((COUNTIF($A$2:A2,A2)-1)=0,0,SUM(OFFSET(F2,-1*(COUNTIF($A$2:A2,A2)-1),0,COUNTIF($A$2:A2,A2)-1)))),SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&","),LEN(IFERROR(SUBSTITUTE(SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&",",",","|",IF((COUNTIF($A$2:A2,A2)-1)=0,0,SUM(OFFSET(F2,-1*(COUNTIF($A$2:A2,A2)-1),0,COUNTIF($A$2:A2,A2)-1)))),SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&","))-FIND("|",IFERROR(SUBSTITUTE(SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&",",",","|",IF((COUNTIF($A$2:A2,A2)-1)=0,0,SUM(OFFSET(F2,-1*(COUNTIF($A$2:A2,A2)-1),0,COUNTIF($A$2:A2,A2)-1)))),SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&","))),IFERROR(SUBSTITUTE(SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&",",",","|",IF((COUNTIF($A$2:A2,A2)-1)=0,0,SUM(OFFSET(F2,-1*(COUNTIF($A$2:A2,A2)-1),0,COUNTIF($A$2:A2,A2)-1)))),SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&",")),",","]",F2))-1)
想法:a,b,c,d
> a,b,c,d,
> a,b|c,d,
> c,d,
> c,d]
> c,d
> AppleX - c,d
| sum、offset 和 countif 用于“跟踪”以前的计数,并用于放置|
和]
标记。
视觉上:
使用公式(向下拖动):
I2 : =SUBSTITUTE(D2,LEFT(D2,FIND("-",D2)+1),"")&","
J2 : =IF((COUNTIF($A$2:A2,A2)-1)=0,0,SUM(OFFSET(F2,-1*(COUNTIF($A$2:A2,A2)-1),0,COUNTIF($A$2:A2,A2)-1)))
K2 : =IFERROR(SUBSTITUTE(I2,",","|",J2),I2)
L2 : =IFERROR(RIGHT(K2,LEN(K2)-FIND("|",K2)),K2)
M2 : =SUBSTITUTE(L2,",","]",F2)
N2 : =LEFT(M2,FIND("]",M2)-1)
如果它可理解/有效/无效,请分享。