凝结查找/replaceExcel脚本

我期待凝聚我的剧本,因为我还有很长的路要走,即使复制粘贴也需要很长时间。 我只是想缩小查找/replace函数

Function ZoneChanges() Dim MyCell As range Worksheets("Sheet1").Activate Set MyCell = Application.InputBox(Prompt:="Select a cell", Type:=8) MyCell.Replace What:="EE", Replacement:="DA", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False MyCell.Replace What:="EF", Replacement:="DB", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False MyCell.Replace What:="EG", Replacement:="DC", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False MyCell.Replace What:="EH", Replacement:="DD", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False End Function 

谢谢!

如果你必须做多个查找和replace,你可以把所有的值放在数组中并运行一个循环。 问题是,这会比现在慢。 但是,纯粹为了缩短代码,你可以做到这一点。

 Function ZoneChanges() Dim MyCell As Range Dim arrWhat, arrRep, i As Long Worksheets("Sheet1").Activate Set MyCell = Application.InputBox(prompt:="Select a cell", Type:=8) arrWhat = Array("EE", "EF", "EG", "EH"): arrRep = Array("DA", "DB", "DC", "DD") For i = LBound(arrWhat) To UBound(arrWhat) MyCell.Replace What:=arrWhat(i), Replacement:=arrRep(i), LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False Next End Function