在 Excel VBA 中搜索并替换为通配符
Search and replace with wildcards in Excel VBA
我使用逗号作为小数点分隔符,但有时我收到值低于设定限制的文件,然后文件使用点作为“<2.5”。小数点前有时是一位,有时是两位。
我需要能够在以 "less than" 符号开头的单元格中用逗号替换点,但保留实际数字,以便将“<2.5”替换为“<2,5” " 和 "<10.0" 被替换为 "<10,0"。这需要在 Excel VBA.
中完成
我无法对“.”进行一般搜索。并替换为“,”,因为有些地方我需要保持原样。
有人知道如何实现吗?
通过Replace
函数的方法
您可以将数据读入 数据字段数组 ,通过 Replace
函数替换提到的“<”数据,然后通过以下代码在一个语句中将它们写回. - 当然也可以使用 RegEx,如上面的评论所述。
备注
a) 我假设您通过 Set rng = ws.Range("A1:A" & n)
使用 A:A 列中的数据;这可以很容易地更改为任何其他范围。
b) 将值分配给变体数据字段数组会自动创建一个基于一的 2-dim 数组,您可以在只有一列的情况下解决这个问题,例如通过 v(1,1)
、v(2,1)
、v(3,1)
等到 v(n,1)
.
示例代码
Option Explicit
Sub replaceLowerThan()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("MySheet") ' << Change to your sheet name
Dim n As Long, i As Long
Dim rng As Range
Dim v
' get last row number and define data range
n = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set rng = ws.Range("A1:A" & n)
' write data to 1-based 2-dim datafield array
v = rng.Value2
' replace "<..." values
For i = 1 To n
If Not IsError(v(i, 1)) Then ' omit cells with errors like #DIV/0!
If v(i, 1) Like "<*" Then v(i, 1) = Replace(v(i, 1), ".", ",")
End If
Next i
' write values back
rng.Value2 = v
End Sub
这有效:
Dim strPattern As String: strPattern = "(<[0-9]+)[\.]"
Dim strReplace As String: strReplace = ","
Dim myreplace As Long
Dim strInput As String
Dim Myrange As Range
Set RegEx = CreateObject("VBScript.RegExp")
Set Myrange = ActiveSheet.UsedRange
For Each cell In Myrange
If strPattern <> "" Then
strInput = cell.Value
With RegEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If RegEx.Test(strInput) Then
cell.Value = (RegEx.Replace(strInput, strReplace))
End If
End If
Next
我使用逗号作为小数点分隔符,但有时我收到值低于设定限制的文件,然后文件使用点作为“<2.5”。小数点前有时是一位,有时是两位。
我需要能够在以 "less than" 符号开头的单元格中用逗号替换点,但保留实际数字,以便将“<2.5”替换为“<2,5” " 和 "<10.0" 被替换为 "<10,0"。这需要在 Excel VBA.
中完成我无法对“.”进行一般搜索。并替换为“,”,因为有些地方我需要保持原样。
有人知道如何实现吗?
通过Replace
函数的方法
您可以将数据读入 数据字段数组 ,通过 Replace
函数替换提到的“<”数据,然后通过以下代码在一个语句中将它们写回. - 当然也可以使用 RegEx,如上面的评论所述。
备注
a) 我假设您通过 Set rng = ws.Range("A1:A" & n)
使用 A:A 列中的数据;这可以很容易地更改为任何其他范围。
b) 将值分配给变体数据字段数组会自动创建一个基于一的 2-dim 数组,您可以在只有一列的情况下解决这个问题,例如通过 v(1,1)
、v(2,1)
、v(3,1)
等到 v(n,1)
.
示例代码
Option Explicit
Sub replaceLowerThan()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("MySheet") ' << Change to your sheet name
Dim n As Long, i As Long
Dim rng As Range
Dim v
' get last row number and define data range
n = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set rng = ws.Range("A1:A" & n)
' write data to 1-based 2-dim datafield array
v = rng.Value2
' replace "<..." values
For i = 1 To n
If Not IsError(v(i, 1)) Then ' omit cells with errors like #DIV/0!
If v(i, 1) Like "<*" Then v(i, 1) = Replace(v(i, 1), ".", ",")
End If
Next i
' write values back
rng.Value2 = v
End Sub
这有效:
Dim strPattern As String: strPattern = "(<[0-9]+)[\.]"
Dim strReplace As String: strReplace = ","
Dim myreplace As Long
Dim strInput As String
Dim Myrange As Range
Set RegEx = CreateObject("VBScript.RegExp")
Set Myrange = ActiveSheet.UsedRange
For Each cell In Myrange
If strPattern <> "" Then
strInput = cell.Value
With RegEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If RegEx.Test(strInput) Then
cell.Value = (RegEx.Replace(strInput, strReplace))
End If
End If
Next