使用 Excel 进行字符串操作 - 如果存在字符串的一部分,如何删除其中的一部分?
我做了一些谷歌搜索,但找不到任何东西,尽管也许我只是找错了地方。 我也不太擅长 VBA,但我确信我可以用正确的指针来解决它:)
我正在构建一个字符串,它是基于各种条件的各种单元格的串联。 我按顺序打了这些。
=IF(A405<>A404,G405,G405&H404)
我想要做的是返回我的串联列表,如果取代者在列表中,则删除被取代的值。
例如,请参阅以下列表:
A, D, G, Y, Z
我想删除 D
if 和 only 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
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.
从末尾开始,在字符串的开头进行添加,并且仅在 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.
我猜 D 可能出现在任何地方,那么怎么样:
I guess D could appear anywhere, so how about:
如果要删除的这些组合不是太多,可以使用 =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).
我也刚刚通过电子邮件得到了这个可能的解决方案:(
它具有带有优先规则的“真实”值)
它看起来与 @Joseph 的答案。
有更好的解决方案吗?
I just got this as a possible solution via email, too:
(this has the "real" values with precedence rules)
It looks relatively similar to @Joseph's answer.
Is there a better solution?
尝试:
但这假设 D 后面始终有逗号和空格。
Try:
But that assumes you always have the comma and space following the D.
首先,为什么不在遍历所有单元格时保留一个字符串数组,然后在最后将它们连接起来?
否则,您将使用 INSTR 和 MID 等字符串函数来执行以下操作:
但是,正如我所说,我会保留一个易于循环的数组,然后一旦您拥有了您知道将使用的所有值,只需最后将它们连接起来。
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:
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.