Excel-查找并替换多个单词
我只想对多个字符串进行简单的查找和替换。例如,我需要将所有“A1”、“A2”、“A3”替换为“系统”,将所有“B1”、“B2”替换为“ACC”,等等 有人知道好的路线吗?我只是不知道如何开始。谢谢你的帮助 底部更新回应Michael的评论对于许多模式替换来说,这是一种更好的方法 如果使用Excel菜单中的手动Excel-查找并替换多个单词,excel,replace,find,vba,Excel,Replace,Find,Vba,我只想对多个字符串进行简单的查找和替换。例如,我需要将所有“A1”、“A2”、“A3”替换为“系统”,将所有“B1”、“B2”替换为“ACC”,等等 有人知道好的路线吗?我只是不知道如何开始。谢谢你的帮助 底部更新回应Michael的评论对于许多模式替换来说,这是一种更好的方法 如果使用Excel菜单中的手动Replace选项录制一个简单的宏,您将得到可以整理的代码 第一个选项将更新ActiveSheet中包含“我是A1”的单元格到“我是系统”-部分字符串匹配 第二个选项将只将只包含“A1”的活
Replace
选项录制一个简单的宏,您将得到可以整理的代码
ActiveSheet
中包含“我是A1”
的单元格到“我是系统”
-部分字符串匹配“A1”
的活动表中的单元格更新为“系统”
——即整个单元格字符串匹配
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
更新
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
下面的代码
计时器
比较替换从A1-A99
到B1-B99
- 上面的
方法在一个循环中调用了198次(即2*99)Replace
\variant数组组合RegExp
的相对速度。有关RegExp的更多信息
更多的单元格也将提高朝着替换
方向移动的相对速度。朝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
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
2)Regexp-变量数组子
Sub UpdatePartial(StrIn As String, StrOut As String)
ActiveSheet.UsedRange.Replace StrIn, StrOut, xlPart
End Sub
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
“A2”是字符串还是单元格引用?如果我只想在一列中更新它怎么办?我会在activesheet中使用类似列(“C:C”)的内容吗?您可以在activesheet.Columns(“A:A”)中使用,而不是activesheet.UsedRange
。但是在运行之前,应该通过xl菜单将查找和替换选项设置为工作表
,而不是工作簿
。如果使用Regex,则只需编写两条替换语句。您将搜索[0-9]{1,2},而不是A1、A2、A3。这将找到A1-A99。与B:B[0-9]{1,2}相同。这样,如果您的数据集变大/变小,您就不必一直编写replace语句了。@tmoore82除非OP没有要求从A1-A99进行匹配。另外,您实际上不能直接将Regexp与Excel VBA替换一起使用。@brettdj很棒的东西!看到不同的方法排列在一起真的很有用。谢谢你的邀请!:)