Excel - VBA:自动替换单词(使用 What/Replacement)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17016085/
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 - VBA : automatically replace words with another (using What/Replacement)
提问by Phalanx
I would like my code to automatically replace some words from a list by other words. So far, I found how to replace one word by another. For example, if I want the word "Avenue" to be replaced by the word "Ave." I use :
我希望我的代码自动用其他单词替换列表中的某些单词。到目前为止,我找到了如何用一个词替换另一个词。例如,如果我希望将“Avenue”一词替换为“Ave”一词。我用 :
Worksheets("sMain").Columns("D").Replace _
What:="Avenue", Replacement:="Ave.", _
SearchOrder:=xlByColumns, MatchCase:=True
It works. Now I would like to make it more efficient by replacing more than one word by another. For example, if I type "Avenue" or "avenue" or "ave.", it will be replaced by "Ave.". I have been trying this but with no success (compilation error) :
有用。现在我想通过用另一个词替换多个词来提高效率。例如,如果我输入“Avenue”或“avenue”或“ave.”,它将被“Ave.”替换。我一直在尝试这个,但没有成功(编译错误):
Worksheets("sMain").Columns("D").Replace _
What:="Avenue", Replacement:="Ave.", _
What:="avenue", Replacement:="Ave.", _
What:="ave.", Replacement:="Ave.", _
SearchOrder:=xlByColumns, MatchCase:=True
Any idea on how to make it possible? Thanks
关于如何使其成为可能的任何想法?谢谢
回答by Wild138
You could do it in a loop like this
你可以像这样循环
ary = Array("Avenue", "avenue", "ave.")
For Each wd In ary
Columns("D").Replace What:=wd, Replacement:="Ave.", SearchOrder:=xlByColumns, MatchCase:=True
Next
You may also like to set MatchCase to False as you won't need the two upper and lower case "avenue"'s .
您可能还想将 MatchCase 设置为 False,因为您不需要两个大写和小写的 "avenue"'s 。