我参加聚会有点晚了,但也许这会有所帮助。问题中的链接有一个类似的公式,但我的使用 IF() 语句来消除错误。
如果你不怕 Ctrl+Shift+Enter,你可以用数组公式做得很好。
字符串(在单元格 A1 中):
“一二三四”
公式:
{=MAX(IF(MID(A1,ROW($1:$99),1)=".",ROW($1:$99)))} use Ctrl+Shift+Enter
结果:14
首先,
ROW($1:$99)
返回一个从 1 到 99 的整数数组:{1,2,3,4,...,98,99}。
接下来,
MID(A1,ROW($1:$99),1)
返回目标字符串中找到的长度为1的字符串数组,达到目标字符串长度后返回空白字符串:{"o","n","e",".",..."u","r","","",""...}
接下来,
IF(MID(I16,ROW($1:$99),1)=".",ROW($1:$99))
将数组中的每一项与字符串“.”进行比较并返回字符串中字符的索引或 FALSE:{FALSE,FALSE,FALSE,4,FALSE,FALSE,FALSE,8,FALSE,FALSE,FALSE,FALSE,FALSE,14,FALSE,FALSE.....}
最后,
=MAX(IF(MID(I16,ROW($1:$99),1)=".",ROW($1:$99)))
返回数组的最大值:14
这个公式的优点是它很短,比较容易理解,并且不需要任何独特的字符。
缺点是需要使用 Ctrl+Shift+Enter 以及字符串长度的限制。这可以通过下面显示的变体来解决,但该变体使用 OFFSET() 函数,它是一个易失(读取:慢)函数。
不确定这个公式与其他公式相比的速度如何。
变化:
=MAX((MID(A1,ROW(OFFSET($A$1,,,LEN(A1))),1)=".")*ROW(OFFSET($A$1,,,LEN(A1)))) works the same way, but you don't have to worry about the length of the string
=SMALL(IF(MID(A1,ROW($1:$99),1)=".",ROW($1:$99)),2) determines the 2nd occurrence of the match
=LARGE(IF(MID(A1,ROW($1:$99),1)=".",ROW($1:$99)),2) determines the 2nd-to-last occurrence of the match
=MAX(IF(MID(I16,ROW($1:$99),2)=".t",ROW($1:$99))) matches a 2-character string **Make sure you change the last argument of the MID() function to the number of characters in the string you wish to match!