我有一个列范围(以文本形式存储),只有以"-fsa“结尾的单元格,必须将第二个最后一个数字从7替换为5。下面是一个例子
发现
52881871-fsa
替换
52881851-fsa
我尝试使用通配符,但是它只在find函数中工作,而在替换函数中不工作。
谢谢你的帮助!
我在我的代码中使用了下面的方法,find做了所要求的事情,但没有替换
Selection.Replace What:=("??????7?-fsa"), Replacement:=("??????5?-fsa"), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
False, ReplaceFormat:=False
发布于 2022-05-11 09:23:45
只能在查找模式中使用通配符,而不能在替换模式中使用通配符。您需要使用Selection.Find
而不是.Replace
。然后拆分找到的字符串,用一个7
替换5
。
Dim FoundAt As Range
Set FoundAt = Selection.Find(What:=("??????7?-fsa"), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
False)
If Not FoundAt Is Nothing Then
FoundAt.Value = Left$(FoundAt.Value, 6) & "5" & Right$(FoundAt.Value, 5)
End If
另外,您也可以使用正则表达式来完成此任务:How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
https://stackoverflow.com/questions/72198104
复制相似问题