Excel公式查找单元格中的第一个非字母字符?

发布于 2024-08-06 07:36:22 字数 415 浏览 4 评论 0原文

有没有办法使用 Excel 中的单元格公式来查找引用单元格中第一个非字母字符的索引?

我已将数据导入到 Excel 中,其中有一列如下所示:

<派生单元格中我不想要的某些文本><在我想要的文本前面添加并且始终为常量的某些文本>TheTextIWant<一些非字母字符><一些可能是字母或不是字母的其他字符>

我正在尝试创建一个到目前为止看起来像这样的公式:

=MID(<单元格引用>, SEARCH ("<前置文本>, <单元格引用>) + <前置文本的长度>, ??)

其中 ?? 是一些子公式,用于查找“TheTextIWant”之后的第一个非字母字符

这可能吗?

Is there a way using a cell formula in Excel to find the index of the first non-alpha character in a referenced cell?

I've imported data into Excel which has a column that looks like this:

<some text I don't want in the derived cell><some text that prepends the text I want and is always constant>TheTextIWant<Some non-alpha character><some other characters that may be alpha or not>

I'm trying to create a formula that so far looks like this:

=MID(<cell reference>, SEARCH("<prepended text>, <cell reference>) + <length of prepended text>, ??)

Where ?? is some sub-formula that finds the first non-alpha character after "TheTextIWant"

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

殤城〤 2024-08-13 07:36:23

下面的公式从字符串中提取数字,类似的方法也可用于提取非字母字符,只需将所需的字符而不是数字放在括号中即可

=RIGHT(D3,LEN(D3)+1-MIN(FIND({1,2,3,4,5,6,7,8,0,9},D3&"0123456789")))

The following formula extracts numbers from the string , similar way can be used to extract non alpha characters , just place the characters required instead of the numbers in the bracket

=RIGHT(D3,LEN(D3)+1-MIN(FIND({1,2,3,4,5,6,7,8,0,9},D3&"0123456789")))
太阳哥哥 2024-08-13 07:36:22

要更详细地了解数组公式的工作原理(@Jacob 的公式在某些情况下似乎略有错误,例如 0 代表“A1A”),所以这是我认为是一个工作版本,为了便于使用而进行了扩展阅读。请让我知道任何它不起作用的例子。

=IF(
    LEN(A1)=0
    ,0
    ,MIN(
        IF(
            ISNUMBER(
                1*MID(
                    A1,
                    ROW(INDIRECT("A1:A"&LEN(A1))),
                    1
                )
            ),
            ROW(INDIRECT("A1:A"&LEN(A1))),
            LEN(A1)+1
        )
    )
)
  1. 其核心是 row() 函数,在数组模式下,该函数对范围 a1:a*n* 中的每个单元格计算一次,其中 n 是中字符串的长度单元格 A1,创建数组 {1,2,...,n}。
  2. 该数组被传递给 MID 函数,该函数又生成一个数组 {*char_1*,*char_2*,...,*char_n*}。
  3. 该数组会乘以 1,以防止数字字符在后续步骤中被解析为文本。这也可以使用 VALUE() 来完成。
  4. 然后它被传递给 ISNUMBER() 函数,该函数输出逻辑值数组。

这是传递给 IF 的条件数组。然后通过重复步骤 1 构建第二个数组,其中包含每个字符的位置。 IF 函数在数组模式下生成输出数组。条件为 TRUE 的情况被设置为字符位置数组中的相应值,其余的全部设置为 LEN(A1)+1,因此它们大于任何数字字符的任何可能位置值。

然后,MIN 函数计算 IF 输出的数组,返回最小值,即单元格 A1 中字符串中第一个数字字符的位置。如果单元格中没有文本,最外面的 IF 函数会强制公式返回 0,而不是 1;否则,间接返回#REF!搜索单元格 A0 时出错,向上传播,IF 返回 FALSE 条件,返回 0 + 1 = 1。

要搜索第一个非数字字符的位置,只需颠倒顺序即可IF 的后 2 个参数:

=IF(
    LEN(A1)=0
    ,0
    ,MIN(
        IF(
            ISNUMBER(
                1*MID(
                    A1,
                    ROW(INDIRECT("A1:A"&LEN(A1))),
                    1
                )
            ),
            LEN(A1)+1,
            ROW(INDIRECT("A1:A"&LEN(A1)))

        )
    )
)

To go into a bit more detail about how the array formula works (@Jacob's formula seems to be slightly wrong in a couple of cases, e.g. 0 for "A1A"), so here's what I think is a working version, expanded for ease of reading. Please let me know of any examples where it doesn't work.

=IF(
    LEN(A1)=0
    ,0
    ,MIN(
        IF(
            ISNUMBER(
                1*MID(
                    A1,
                    ROW(INDIRECT("A1:A"&LEN(A1))),
                    1
                )
            ),
            ROW(INDIRECT("A1:A"&LEN(A1))),
            LEN(A1)+1
        )
    )
)
  1. At the heart of this is the row() function, which in array mode is evaluated once for each of the cells in the range a1:a*n*, where n is the length of the string in cell A1, creating an array {1,2,...,n}.
  2. The array is passed to the MID function, which in turn produces an array {*char_1*,*char_2*,...,*char_n*}.
  3. The array is multiplied by 1 to prevent numeric characters being parsed as text in subsequent steps. This could also be done using VALUE().
  4. It is then passed to the ISNUMBER() function, which outputs an array of logical values.

This is the array of conditions passed to IF. A second array is then constructed by repeating step 1, containing the position of each character. The IF function, in array mode, produces an array of outputs. Cases whose condition is TRUE are set to the corresponding value from the array of character positions, and the rest are all set to LEN(A1)+1 so they are larger than any possible position values for any numeric characters.

The MIN function then evaluates the array output by IF, returning the smallest value, which is the position of the first numeric character in the string in cell A1. The outermost IF function forces the formula to return 0 if there is no text in the cell, rather than 1; otherwise, INDIRECT returns a #REF! error when searching for cell A0, which propagates upwards, and the FALSE condition is returned by IF, returning 0 + 1 = 1.

To search for the position of the first non-numeric character instead, all you have to do is reverse the order of the second 2 arguments to IF:

=IF(
    LEN(A1)=0
    ,0
    ,MIN(
        IF(
            ISNUMBER(
                1*MID(
                    A1,
                    ROW(INDIRECT("A1:A"&LEN(A1))),
                    1
                )
            ),
            LEN(A1)+1,
            ROW(INDIRECT("A1:A"&LEN(A1)))

        )
    )
)
一袭水袖舞倾城 2024-08-13 07:36:22

在此页面有一个数组公式,用于查找“A 中第一个数字的位置”细绳。”您应该能够修改它以使用“T”函数(以确定字符是否是文本),结合一点减法,然后得到您正在寻找的内容。

On this page there is an array formula for finding the "Position Of First Digit In A String." You should be able to modify that to use the 'T' function (to determine if a character is text), incorporate a little subtraction and then get to what you're looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文