是否可以将下面的内容替换为用引号引起来的所有内容,包括什么都不要或“”,将其替换为任何可能的字母(A至Z)以及之后的“”?所以像
Replace What:="A-Z"*", Replacement:=""
我遇到一个问题,其中字段是从旧的旧系统中串联出来的,因此例如出现456P321345134,但我只需要456,之后就什么也不需要。
也许可以将Not(Numeric)集成到某个字符串中吗?它不一定是第三个字符,字母可以是第二,第三,第四等等。
原文:
Replace What:="""*", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
答案 0 :(得分:2)
没有RegEx,可以执行以下简单操作:
For i = 97 To 122
Range("your range here").Replace Chr(i) & "*", "", 2, 1, 0
Next
答案 1 :(得分:1)
Option Explicit
Sub firstNumsOnly()
Dim i As Long, vals As Variant
Dim rgx As Object
Set rgx = CreateObject("VBScript.RegExp")
With Worksheets("sheet4")
vals = .Range(.Cells(1, "A"), .Cells(.Rows.Count, "A").End(xlUp)).Value2
With rgx
.Global = True
.MultiLine = False
.Pattern = "[A-Z]"
For i = LBound(vals, 1) To UBound(vals, 1)
vals(i, 1) = .Replace(vals(i, 1), ChrW(8203))
vals(i, 1) = Split(vals(i, 1) & ChrW(8203), ChrW(8203))(0)
Next i
End With
.Cells(1, "A").Resize(UBound(vals, 1), UBound(vals, 2)) = vals
End With
End Sub
答案 2 :(得分:1)
下面的函数应该合并到一个宏中,该宏在您期望的范围内遍历每个单元以进行处理。
使用正则表达式(如果没有前导数字,将返回VALUE#
。
Option Explicit
Function firstNums(S As String)
Dim RE As Object, MC As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = False
.MultiLine = True
.Pattern = "^\d+" 'Match all digits starting at the beginning of string
If .test(S) Then
Set MC = .Execute(S)
firstNums = MC(0)
Else
firstNums = CVErr(xlErrValue)
End If
End With
End Function
如果您希望返回一个完全空白的字符串,如果没有前导数字,则:
Option Explicit
Function firstNums(S As String)
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = False
.MultiLine = True
.Pattern = "(^\d*)\D.*"
firstNums = .Replace(S, "$1")
End With
End Function