使用 Excel 进行字符串操作 - 如果存在字符串的一部分,如何删除其中的一部分?

发布于 2024-07-08 12:56:24 字数 455 浏览 6 评论 0原文

我做了一些谷歌搜索,但找不到任何东西,尽管也许我只是找错了地方。 我也不太擅长 VBA,但我确信我可以用正确的指针来解决它:)

我正在构建一个字符串,它是基于各种条件的各种单元格的串联。 我按顺序打了这些。

=IF(A405<>A404,G405,G405&H404)

我想要做的是返回我的串联列表,如果取代者在列表中,则删除被取代的值。

例如,请参阅以下列表:

A, D, G, Y, Z

我想删除 D ifonly if Y< /code> 存在。

我该怎么办呢? (VBA 或 in-cell,但我更喜欢 in-cell)

I've done some Googling, and can't find anything, though maybe I'm just looking in the wrong places. I'm also not very adept at VBA, but I'm sure I can figure it out with the right pointers :)

I have a string I'm building that's a concatenation of various cells, based on various conditions. I hit these in order.

=IF(A405<>A404,G405,G405&H404)

What I want to do is go back through my concatenated list, removing a superseded value if the superseder is in the list.

For example, see the following list:

A, D, G, Y, Z

I want to remove D if and only if Y is present.

How would I go about this? (VBA or in-cell, though I'd prefer in-cell)

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

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

发布评论

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

评论(7

九局 2024-07-15 12:56:25

VBA:您始终可以使用正则表达式对象。
我认为只要正确构建正则表达式,您就能够测试脚本上的任何内容。

查看:http:// /msdn.microsoft.com/en-us/library/yab2dx62(VS.85).aspx(用于正则表达式参考)
以及一个测试正则表达式的简单工具: http://www.codehouse.com/webmaster_tools/regex/

单元格内:您可以以更适合 Excel 的方式进行操作:
假设 A:A 列上有值。
您可以添加一个新列来执行检查
if(间接("A"&row()) <> 间接("A"&row()-1), 间接("G"&row()), 间接("G" "&row())& 间接("H"&row()))
或任何值。 但我猜想,在 if 语句的一个分支上,该值应该为空。 之后,仅连接 B:B 列值(如果需要,跳过空格)。

希望这可以帮助。

VBA : You can always use the regexp object.
I think that gives you the ability to test anything on your script as long as you build correctly the regular expression.

Check out : http://msdn.microsoft.com/en-us/library/yab2dx62(VS.85).aspx ( for regexp reference )
and a simple tool to test your regexps : http://www.codehouse.com/webmaster_tools/regex/

In-cell: you could do it in a more excel friendly way:
suppose on column A:A you have the values.
You can add a new column where you perform the check
if(indirect("A"&row()) <> indirect("A"&row()-1), indirect("G"&row()), indirect("G"&row())& indirect("H"&row()))
or whatever the values are. I guess however that on one branch of the if statement the value should be blank. After that you concatenate only the B:B column values ( skipping blanks if needed ).

Hope this helps.

电影里的梦 2024-07-15 12:56:25

从末尾开始,在字符串的开头进行添加,并且仅在 Y 不存在时才添加 D 可能更容易。

It's probably easier to start at the end, make your additions to the beginning of the string, and only add D if Y is not present.

笑饮青盏花 2024-07-15 12:56:25

我猜 D 可能出现在任何地方,那么怎么样:

If InStr(strString, "Y") > 0 Then
   strString = Replace(strString, "d", "")
   strString = Replace(strString, "  ", "")
   strString = Replace(strString, " ,", "")
   strString = Replace(strString, ",,", ",")
End If

I guess D could appear anywhere, so how about:

If InStr(strString, "Y") > 0 Then
   strString = Replace(strString, "d", "")
   strString = Replace(strString, "  ", "")
   strString = Replace(strString, " ,", "")
   strString = Replace(strString, ",,", ",")
End If
等待圉鍢 2024-07-15 12:56:25

如果要删除的这些组合不是太多,可以使用 =IF(FIND("D"; A2)> 0; REPLACE(A2;1;3;"");A2)。

If there are not too many of these combinations that you want to remove, you can use =IF(FIND("D"; A2)> 0; REPLACE(A2;1;3;"");A2).

剧终人散尽 2024-07-15 12:56:25

我也刚刚通过电子邮件得到了这个可能的解决方案:(

=IF(A15<>A14,G15,IF(OR(AND(G15="CR247, ",ISNUMBER(FIND("CR247, ",H14))),AND(G15="CR149, ",ISNUMBER(FIND("CR215, ",H14))),AND(G15="CR149, ",ISNUMBER(FIND("CR180, ",H14))),AND(G15="CR180, ",ISNUMBER(FIND("CR215, ",H14))),G15="CR113, "),H14,G15&H14))

它具有带有优先规则的“真实”值)

它看起来与 @Joseph 的答案。

有更好的解决方案吗?

I just got this as a possible solution via email, too:

=IF(A15<>A14,G15,IF(OR(AND(G15="CR247, ",ISNUMBER(FIND("CR247, ",H14))),AND(G15="CR149, ",ISNUMBER(FIND("CR215, ",H14))),AND(G15="CR149, ",ISNUMBER(FIND("CR180, ",H14))),AND(G15="CR180, ",ISNUMBER(FIND("CR215, ",H14))),G15="CR113, "),H14,G15&H14))

(this has the "real" values with precedence rules)

It looks relatively similar to @Joseph's answer.

Is there a better solution?

你没皮卡萌 2024-07-15 12:56:24

尝试:

=IF(ISERROR(FIND("Y",A1)),A1,SUBSTITUTE(A1,"D, ",""))

但这假设 D 后面始终有逗号和空格。

Try:

=IF(ISERROR(FIND("Y",A1)),A1,SUBSTITUTE(A1,"D, ",""))

But that assumes you always have the comma and space following the D.

芸娘子的小脾气 2024-07-15 12:56:24

首先,为什么不在遍历所有单元格时保留一个字符串数组,然后在最后将它们连接起来?

否则,您将使用 INSTR 和 MID 等字符串函数来执行以下操作:

start1 = instr(myLongString,"Y, ")
if start1 > 0 Then
    start2 = instr(myLongString,"D, ")
    if start2 > 0 then
        newLongString = left(myLongString, start2 - 1) & _
                        mid(myLongString, start2 + 3)
    end if
end if

但是,正如我所说,我会保留一个易于循环的数组,然后一旦您拥有了您知道将使用的所有值,只需最后将它们连接起来。

Firstly, why not keep a string array instead as you go through all the cells, then concatenate it all at the end?

Otherwise, you'll be using string functions like INSTR and MID to do something like:

start1 = instr(myLongString,"Y, ")
if start1 > 0 Then
    start2 = instr(myLongString,"D, ")
    if start2 > 0 then
        newLongString = left(myLongString, start2 - 1) & _
                        mid(myLongString, start2 + 3)
    end if
end if

But, as I said, I would keep an array that is easy to loop through, then once you have all the values you KNOW you will use, just concatenate them at the end.

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