vba Excel:如何使用通配符替换文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/26280008/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel: How do I replace text by using a wildcard?
提问by helena4
A1= Blabla,124814012
A1=布拉布拉,124814012
I want to remove everything but "Blabla".
我想删除除“ Blabla”之外的所有内容。
What command do i use to get the effect as if i did CTRL+H (replace) and told it to replace ",*" with nothing.
我用什么命令来获得效果,就好像我做了 CTRL+H(替换)并告诉它什么都不替换“,*”。
I can do it this: =REPLACE(A1,FIND(",",A1,1),3000,"")
我可以这样做: =REPLACE(A1,FIND(",",A1,1),3000,"")
But need to be able to do it using x* etc.
但是需要能够使用 x* 等来做到这一点。
采纳答案by user3616725
get Morefunc Addon and use:
获取 Morefunc 插件并使用:
=REGEX.SUBSTITUTE(A1 , ",(.*)" , "")
=REGEX.SUBSTITUTE(A1 , ",(.*)" , "")
MOREFUNC ADDON
更多功能插件
- Morefunc Addon is a free library of 66 new worksheet functions.
- HEREis some information (by original author)
- here is the last working download linkI found
- here is a good installation walk-through video
回答by helena4
Try,
尝试,
Dim x as string
x = range("a1").value
if cbool(instr(1, x, chr(44))) then _
    x = left(x, instr(1, x, chr(44))-1)  'Chr(44) is a comma
debug.print x
Addendum:I'd make the function version a little fancier.
附录:我会让函数版本更漂亮一些。
Public Function fc_Split_at(sSTR As String, sDELIM As String, Optional iPC = 1)
    fc_Split_at = Split(sSTR, sDELIM)(iPC - 1)
End Function
Syntax:
句法:
=fc_Split_at(<original string>, <delimiter>, [optional]<piece to return>)
=fc_Split_at( <原始字符串>, <分隔符>, [可选] <要返回的片段>)
?fc_Split_at("*Blabla,124814012", ",")
*Blabla
?fc_Split_at("*Blabla,124814012", ",", 2)
124814012
回答by John Bustos
Excel's Find & Replace does allow for wildcard characters.
Excel 的查找和替换允许使用通配符。
In your simplified example, if you went to find / replace and typed in:
在您的简化示例中,如果您去查找/替换并输入:
Find What:Blabla*
找什么:Blabla*
Replace With:Blabla
用。。。来代替:Blabla
That would do the trick and remove anything following Blabla.
这样就可以解决问题并删除Blabla.
Hope that does the trick!!
希望能解决问题!!

