[英]Can find how to use replace with regex in VBA
I have a table in Excel and one of its columns contains string of the type: 125j, 0j, 12j, etc. 我在Excel中有一张表,它的列之一包含类型的字符串:125j,0j,12j等。
I want the value of the cells (in this column) to be replace by the numbers (ie I want to drop the "j") using the VBA. 我希望使用VBA将单元格(在此列中)的值替换为数字(即,我想删除“ j”)。 Here is what I have:
这是我所拥有的:
Dim reg As Object
Set reg = CreateObject("VBScript.RegExp")
reg.IgnoreCase = True
reg.Pattern = "^[0-9]+"
If reg.test(Cells(i, 4).Value) Then
Cells(i, 4).Value = reg.Execute(Cells(i, 4).Value)
End If
But it doesn't work, do anyone knows either how to crrect it or to obtain the same result from another command? 但这是行不通的,有人知道如何压缩它或从另一个命令中获得相同的结果吗?
Thank you 谢谢
The result of the .Execute
method is a MatchCollection
object. .Execute
方法的结果是MatchCollection
对象。 You can use that explicitly: 您可以明确地使用它:
Dim reg As Object
Dim allMatches As Object
Set reg = CreateObject("VBScript.RegExp")
reg.IgnoreCase = True
reg.Pattern = "^[0-9]+"
If reg.test(cells(i, 4).Value) Then
Set allMatches = reg.Execute(cells(i, 4).Value)
cells(i, 4).Value = allMatches(0)
End If
Or implicitly: 或隐式地:
cells(i, 4).Value = reg.Execute(cells(i, 4).Value)(0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.