Dri*_*len 1 worksheet-function string microsoft-excel
以此已解决的问题为基础: 查找字符串中的第一个非数字字符
在该问题中,OP 请求有关 Excel 公式的帮助,该公式将返回字符串中第一个非数字字符的位置。这本身就非常有用,并且获得了一个非常干净的解决方案:
{=MATCH(TRUE,ISERROR(VALUE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))),0)}
Run Code Online (Sandbox Code Playgroud)
问题是我正在寻找此解决方案的变体,但这不会排除小数分母或千分母.和,。
我几个小时以来一直在寻找一个优雅的解决方案,但我还没有完全实现,所以任何帮助将不胜感激!
我有四种方法:
\nTRUE 查找不在VLOOKUP数组中的第一个查找字符,而 FALSE 查找位于 VLOOKUP 数组中的第一个查找字符。
\nA1返回与中的字符不匹配的第一个字符的位置{","; "."; [0-9]}
# CHAR to CODE version\n\n=XMATCH(TRUE,\n ISERROR(VLOOKUP(\n CODE(MID(A1,SEQUENCE(1,LEN(A1)),1)),\n VSTACK(44,46,SEQUENCE(10,,48)),1,0)))\n\n# CODE to CHAR version\n\n=XMATCH(TRUE,\n ISERROR(VLOOKUP(\n MID(A1,SEQUENCE(1,LEN(A1)),1),\n CHAR(VSTACK(44,46,SEQUENCE(10,,48))),1,0)))\n\nRun Code Online (Sandbox Code Playgroud)\n\n\nA1返回与中的字符匹配的第一个字符的位置{[A-Z,a-z]}
# CHAR to CODE version\n\n=XMATCH(FALSE,\n ISERROR(VLOOKUP(\n CODE(MID(A1,SEQUENCE(1,LEN(A1)),1)),\n VSTACK(SEQUENCE(26,,65),SEQUENCE(26,,97)),1,0)))\n\n# Since the lookup in this instance is not case-sensitive,\n# the formula can be simplified further with UPPER (or LOWER)\n\n=XMATCH(FALSE,\n ISERROR(VLOOKUP(\n CODE(UPPER(MID(A1,SEQUENCE(1,LEN(A1)),1))),\n SEQUENCE(26,,65),1,0)))\n\n# CODE to CHAR version\n\n=XMATCH(FALSE,\n ISERROR(VLOOKUP(\n UPPER(MID(A1,SEQUENCE(1,LEN(A1)),1)),\n CHAR(SEQUENCE(26,,65)),1,0)))\nRun Code Online (Sandbox Code Playgroud)\n\'IS\' 字母表中的一个字母(CHAR 到 CODE)
\'IS\' 字母表中的一个字母(CODE 到 CHAR)
CODE("A")=65\xc2\xa0 / \xc2\xa0CHAR(65)="A"
| 字符 | 代码 |
|---|---|
| , | 44 |
| 。 | 46 |
| 0-9 | 48-57 \xc2\xa0\xc2\xa0=SEQUENCE(10,,48) |
| AZ | 65-90 \xc2\xa0\xc2\xa0=SEQUENCE(26,,65) |
| 阿兹 | 97-122\xc2\xa0=SEQUENCE(26,,97) |
使用原始公式并添加多个SUBSTITUTE函数将任何句点和逗号更改为数字,例如0.
SEQUENCE(1,LEN(A1))\xc2\xa0而不是\xc2\xa0 ROW(INDIRECT("1:"&LEN(A1)))=MATCH(TRUE,ISERROR(VALUE(\n SUBSTITUTE(SUBSTITUTE(\n MID(A1,SEQUENCE(1,LEN(A1)),1),\n ".",0),",",0))),0)\nRun Code Online (Sandbox Code Playgroud)\n| A \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 | B \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\ xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\ xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\ xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\ xa0\xc2\xa0\xc2\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7 |
, | 0 | 0 |
a | a | #VALUE! |
我编写了一个名为 LAMBDA 的函数,该函数调用自身(递归)。公式很简单;它测试一个字符串,看看是否可以强制转换为数字,但不能强制转换为日期,如果可以,则返回该字符串,如果不能,则删除字符串中的最后一个字符并重复该过程,直到字符串为空,或者遇到一个数字。
\n这种方法适用于更多类型的数字,例如负数和科学计数法,我过滤了日期和时间,因为它引入了一些额外的问题。它还会根据您计算机的区域设置将字符串解释为数字。
\n该公式依赖于以下两个假设:
\nA1由一个数字后紧跟零个或多个“附加字符”组成我_getNum在 Excel 的“名称管理器”中命名了我的公式。根据需要自定义名称,但请记住,由于公式调用自身,因此公式中的名称需要与保存它的名称相匹配。
# formula returns the number portion of the string\n\n=LAMBDA(str, IF(OR(str="", AND(\n ISERROR(DATEVALUE(str)),\n NOT(ISERROR(VALUE(str))))),str,\n _getNum(LEFT(str,LEN(str)-1))))\nRun Code Online (Sandbox Code Playgroud)\n或者,此_getPos版本将返回数字字符串后下一个字符的位置
=LAMBDA(str, IF(str="", "",\n IF(AND(ISERROR(DATEVALUE(str)),\n NOT(ISERROR(VALUE(str))))),LEN(str)+1,\n _getPos(LEFT(str,LEN(str)-1))))\nRun Code Online (Sandbox Code Playgroud)\n可以根据您的需要进行修改,也可以简单地在单元格公式中进行调整。
\n例如:
\n_getPos(A1)=LEN(_getNum(A1))+1=_getNum(A1)或者=LEFT(A1,_getPos(A1)-1)=SUBSTITUTE(A1,_getNum(A1),"")=RIGHT(A1,LEN(A1)+1-_getPos(A1))此方法利用了逗号和句点是有效数字分隔符这一事实(北美)。在使用 VALUE 函数进行测试之前,1已预先添加并附加到所有字符。000请注意,OP 评论说,该公式的早期版本将字母识别E为有效数字,因为1E000科学记数法1^0等于1。我预计它还有其他不可预见的行为,我最初删除了这种方法以支持替代方案,但后来又恢复了它,因为它是OP选择的方法。
=LET(str,SUBSTITUTE(A16,"E","F"),\n MATCH(TRUE,ISERROR(VALUE(\n "1"&MID(str,SEQUENCE(1,LEN(str)),1)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xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\ xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 |
|---|---|---|
| 特点 - - - - - | =“1”&A#&“000” ------------------------ | =值(B#) --------------- |
. | "1.000" | 1 |
7 | "17000" | 17000 |
, | "1,000" | 1000 |
a | "1a000" | #VALUE! |
\xc2\xa0
\n\xc2\xa0
我在较长的公式中添加了换行符。这使我更容易阅读和调试它们。当宽度受到限制时,它们在代码块中也能更好地显示。
\n大多数函数(除了少数例外)都允许不必要的空格和换行符,除了视觉之外不会产生任何影响。显然,字符串内的空格和换行符都会成为字符串的一部分
\nAlt编辑公式时使用+在公式中插入换行符Enter
\n