2012-11-07 165 views
3

我只想简单地查找并替换多个字符串。例如,我需要将所有“A1”,“A2”,“A3”替换为“系统”,并将所有“B1”,“B2”替换为“ACC”等等......Excel - 查找并替换多个单词

有没有人知道好的路线?我只是不知道如何开始。谢谢您的帮助!

+1

是 “A2” 的字符串或单元格引用? – brettdj

回答

3

更新在底部adressing迈克尔的评论重新许多模式更好的方法替代

如果您使用从Excel菜单手动Replace选项,你会得到的代码,你可以整理一下这个录制一个简单的宏

  1. 比包含"I am A1""I am System"第一个选项将更新ActiveSheet细胞 - 的一部分字符串匹配
  2. 第二个选项w ^生病只更新细胞只含有"A1""Sytem"ActiveSheet - 即全细胞字符串匹配

代码

Sub UpdatePartial() 
With ActiveSheet.UsedRange 
.Replace "A1", "System", xlPart 
.Replace "A2", "System", xlPart 
.Replace "A3", "System", xlPart 
.Replace "B1", "ACC", xlPart 
.Replace "B2", "ACC", xlPart 
End With 
End Sub 

Sub UpdateWhole() 
With ActiveSheet.UsedRange 
.Replace "A1", "System", xlWhole 
.Replace "A2", "System", xlWhole 
.Replace "A3", "System", xlWhole 
.Replace "B1", "ACC", xlWhole 
.Replace "B2", "ACC", xlWhole 
End With 
End Sub 

更新

下面

    代码
  1. 使用一个基本的Timer比较更换所有局部串从A1-A99B1-B99
  2. 这两种方法是
    • 在一个循环
    • RegExpReplace上述方法称为198倍(即2 * 99) \变体阵列组合

在我测试第二方法是由198个替换Ò更快1,000,000个细胞范围。

更少的替代品将提高相对于Replace的速度。更多朝向RegExp 更多细胞也将提高相对于Replace的速度。更少朝向RegExp

我没有继续尝试Find方法,稍后解析字符串。作为hyrbrid型溶液(找到然后解析 UT不会具竞争力到单个替换解析

定时器

Sub MainCaller() 
Dim dbTime As Double 
Dim lngCnt As Long 

dbTime = Timer() 
For lngCnt = 1 To 99 
Call UpdatePartial("A" & lngCnt, "System") 
Call UpdatePartial("B" & lngCnt, "System") 
Next lngCnt 
Debug.Print Timer() - dbTime 
dbTime = Timer() 
Call RegexReplace("(A|B)[1-99]", "System") 
Debug.Print Timer() - dbTime 
End Sub 

1)替换子

Sub UpdatePartial(StrIn As String, StrOut As String) 
ActiveSheet.UsedRange.Replace StrIn, StrOut, xlPart 
End Sub  

2)的正则表达式 - 变量数组子

Sub RegexReplace(StrIn As String, StrOut As String) 
    Dim rng1 As Range 
    Dim rngArea As Range 
    Dim lngRow As Long 
    Dim lngCol As Long 
    Dim lngCalc As Long 
    Dim objReg As Object 
    Dim X() 


    'On Error Resume Next 
    'Set rng1 = Application.InputBox("Select range for the replacement of leading zeros", "User select", Selection.Address, , , , , 8) 
    'If rng1 Is Nothing Then Exit Sub 
    'On Error GoTo 0 

    ActiveSheet.UsedRange 
    Set rng1 = ActiveSheet.UsedRange 

    'See Patrick Matthews excellent article on using Regular Expressions with VBA 
    Set objReg = CreateObject("vbscript.regexp") 
    With objReg 
    .Pattern = StrIn 
    .ignorecase = False 
    .Global = True 
    End With 

    'Speed up the code by turning off screenupdating and setting calculation to manual 
    'Disable any code events that may occur when writing to cells 
    With Application 
     lngCalc = .Calculation 
     .ScreenUpdating = False 
     .Calculation = xlCalculationManual 
     .EnableEvents = False 
    End With 

    'Test each area in the user selected range 

    'Non contiguous range areas are common when using SpecialCells to define specific cell types to work on 
    For Each rngArea In rng1.Areas 
     'The most common outcome is used for the True outcome to optimise code speed 
     If rngArea.Cells.Count > 1 Then 
      'If there is more than once cell then set the variant array to the dimensions of the range area 
      'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks 
      X = rngArea.Value2 
      For lngRow = 1 To rngArea.Rows.Count 
       For lngCol = 1 To rngArea.Columns.Count 
        'replace the leading zeroes 
        X(lngRow, lngCol) = objReg.Replace(X(lngRow, lngCol), StrOut) 
       Next lngCol 
      Next lngRow 
      'Dump the updated array back over the initial range 
      rngArea.Value2 = X 
     Else 
      'caters for a single cell range area. No variant array required 
      rngArea.Value = objReg.Replace(rngArea.Value, StrOut) 
     End If 
    Next rngArea 

    'cleanup the Application settings 
    With Application 
     .ScreenUpdating = True 
     .Calculation = lngCalc 
     .EnableEvents = True 
    End With 

    Set objReg = Nothing 
End Sub 
+0

如果我只想在一列中进行更新,该怎么办?我会用Columns(“C:C”)和activesheet吗? – user960358

+0

您可以使用'With ActiveSheet.Columns(“A:A”)'而不是'ActiveSheet.UsedRange'。但在运行之前,您应该通过xl菜单将查找和替换选项设置为“工作表”而不是“工作簿”。 – brettdj

+0

如果使用正则表达式,则只需编写两条替换语句。代替A1,A2,A3,您可以搜索A [0-9] {1,2}。那会找到A1-A99。与B相同:B [0-9] {1,2}。这样,如果数据集变得更大/更小,则不必永远写入替换语句。 – tmoore82