VBA中的RegExp:如果在一定长度的数字后出现文本/特殊字符,请删除多余的数字
当前我的代码删除单元格值中的所有文本和特殊字符,只保留数字 剩下要做的唯一一件事是在文本/特殊字符出现一定长度后删除数字 例如 41207442(y)2367 我的代码仅将其输出为0412 074 4422367,但应为0412 074 442 删除“(y)”之后的多余数字VBA中的RegExp:如果在一定长度的数字后出现文本/特殊字符,请删除多余的数字,regex,vba,excel,Regex,Vba,Excel,当前我的代码删除单元格值中的所有文本和特殊字符,只保留数字 剩下要做的唯一一件事是在文本/特殊字符出现一定长度后删除数字 例如 41207442(y)2367 我的代码仅将其输出为0412 074 4422367,但应为0412 074 442 删除“(y)”之后的多余数字 Dim strPattern As String:strPattern=“^4(\d\d)(\d\d\d)(\d\d)” Dim strReplace作为字符串:strReplace=“04$1$2$3” 将strPatte
Dim strPattern As String:strPattern=“^4(\d\d)(\d\d\d)(\d\d)”
Dim strReplace作为字符串:strReplace=“04$1$2$3”
将strPattern2设置为字符串:strPattern2=“[^0-9]”
将strereplace2设置为字符串:strereplace2=“”
设置Myrange=ActiveSheet.Range(“A2:A8”)”***将范围更改为确定的文本
对于Myrange中的每个单元格
如果strPattern2“”,则
strInput=cell.Value
用正则表达式
.Global=True
.MultiLine=True
.IgnoreCase=False
.Pattern=strPattern2
以
如果正则表达式测试(strInput),则
'MsgBox(regEx.Replace(strInput,strReplace))
cell.Value=regEx.Replace(strInput,strReplace2)
其他的
'MsgBox(“不匹配”)
如果结束
如果结束
下一个
对于Myrange中的每个单元格
如果strPattern“”则
strInput=cell.Value
用正则表达式
.Global=True
.MultiLine=True
.IgnoreCase=False
.Pattern=strPattern
以
如果正则表达式测试(strInput),则
'MsgBox(regEx.Replace(strInput,strReplace))
cell.Value=regEx.Replace(strInput,strReplace)
其他的
'MsgBox(“不匹配”)
如果结束
如果结束
下一个
对于Myrange中的每个单元格
strInput=cell.Value
如果Len([strInput])12那么
“MsgBox”错误
cell.Interior.ColorIndex=3
其他的
如果结束
如果Len([strInput])=0,则
“MsgBox”错误
cell.Interior.ColorIndex=0
其他的
如果结束
下一个
以
需要帮助,谢谢。您可以稍微更改解决方案的体系结构。因此,以下是:
- 首先查找
,然后在其右侧剪切任何内容(y)
- 然后使用正则表达式删除非数字字符
您的正则表达式模式是什么?所以它也应该插入零?它是如何保持领先地位的?这是文本吗?
strPattern
和strPattern2
的值是多少?你不认为这是相关的吗?抱歉忘了包括:谢谢这个@vityta,但是有可能捕获任何其他文本、数字或特殊字符吗?在某些情况下,前10个数字后面有不同的字母数字字符。
Dim strPattern As String: strPattern = "^4(\d\d)(\d\d\d)(\d\d\d)"
Dim strReplace As String: strReplace = "04$1 $2 $3"
Dim strPattern2 As String: strPattern2 = "[^0-9]"
Dim strReplace2 As String: strReplace2 = ""
Set Myrange = ActiveSheet.Range("A2:A8") '***change range to determined text
For Each cell In Myrange
If strPattern2 <> "" Then
strInput = cell.Value
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern2
End With
If regEx.test(strInput) Then
'MsgBox (regEx.Replace(strInput, strReplace))
cell.Value = regEx.Replace(strInput, strReplace2)
Else
'MsgBox ("Not matched")
End If
End If
Next
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
'MsgBox (regEx.Replace(strInput, strReplace))
cell.Value = regEx.Replace(strInput, strReplace)
Else
'MsgBox ("Not matched")
End If
End If
Next
For Each cell In Myrange
strInput = cell.Value
If Len([strInput]) <> 12 Then
'MsgBox "Error"
cell.Interior.ColorIndex = 3
Else
End If
If Len([strInput]) = 0 Then
'MsgBox "Error"
cell.Interior.ColorIndex = 0
Else
End If
Next
End With
Option Explicit
Public Sub TestMe()
Dim location As Long
location = InStr(1, Range("A1"), "(y") - 1
If location > 0 Then
Range("a1") = Left(Range("a1"), location)
End If
End Sub