vba excel宏来搜索特定的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4756733/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:33:20  来源:igfitidea点击:

excel macro to search for specific string

excelvbaexcel-vba

提问by user434885

i need to write and excel macro to search for a string and replace it with another... how do i do this ? HELP :/

我需要编写和 excel 宏来搜索一个字符串并将其替换为另一个...我该怎么做?帮助 :/

回答by William Zhou

You can record that operations, and Alt+Shift+F11 to open the Script Editor, then reference the code generated by the Recorder. I think that can give you some hints.

您可以记录该操作,并按 Alt+Shift+F11 打开脚本编辑器,然后引用记录器生成的代码。我想这可以给你一些提示。

And I think you don't need to use Macro. It is enough to use the "Find and Replace" Menu Item, and choose the option you want.

而且我认为您不需要使用宏。使用“查找和替换”菜单项并选择您想要的选项就足够了。

回答by Robert Mearns

Using user interfacein Excel 2007

在 Excel 2007 中使用用户界面

  1. Select cell A1
  2. Click on the Home tab in the Ribbon
  3. Click on the 'Find & Select' icon and select 'Replace'
  4. Click on the 'Options' button
  5. Enter the text to search for 'abc' in the 'Find What' box
  6. Enter the text to replace 'abc' with in the 'Replace with' box
  7. Make sure that the tick boxes are not ticked
  8. Make sure Within = Sheet, Search = By Rows and Look in = Formulas
  9. Click on the 'Replace all' button
  1. 选择单元格 A1
  2. 单击功能区中的“主页”选项卡
  3. 单击“查找并选择”图标并选择“替换”
  4. 单击“选项”按钮
  5. 在“查找内容”框中输入要搜索“abc”的文本
  6. 在“替换为”框中输入要替换“abc”的文本
  7. 确保未勾选复选框
  8. 确保内 = 工作表,搜索 = 按行并查看 = 公式
  9. 单击“全部替换”按钮

Replace screenshot

替换截图



Using VBAcode

使用VBA代码

Sub Replace_abc()

    Sheets("Sheet1").Select
    Range("A1").Select
    Cells.Replace What:="abc", Replacement:="def", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

End Sub