VBAreplace号码问题
我有一个问题,我需要用一个贷款标识符来replace一个数字,我遇到的问题是贷款标识符包含需要replace的数字,所以当我真正想要的是“LOAN1592LOAN3161LOAN29269704932LOAN2926970411”贷款编号为6,用LOAN31617932代替上述。 任何想法,我怎么能阻止这种情况发生? 我希望这是有道理的。 请参阅我的代码段如下:
Columns("e:e").Select Selection.Replace What:="5", Replacement:="LOAN15926711", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False Columns("e:e").Select Selection.Replace What:="6", Replacement:="LOAN31617932", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False Columns("e:e").Select Selection.Replace What:="7", Replacement:="LOAN29269704", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False Columns("e:e").Select Selection.Replace What:="8", Replacement:="LOAN57538987", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False
使用xlWhole
:
Sub luxation() Columns("e:e").Replace What:="5", Replacement:="LOAN15926711", LookAt:=xlWhole, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False End Sub
之前:
之后:
注意:
55和555被忽略!
我会做If
(或Select Case
)。 看到我上面的评论为什么。 这是否工作:
Sub t() Dim cel As Range, rng As Range Dim lastRow& lastRow = Cells(Rows.Count, 5).End(xlUp).row Set rng = Range("E1:E" & lastRow) For Each cel In rng If InStr(1, cel.Value, "2") Then cel.Value = "LOAN15926711" If InStr(1, cel.Value, "6") Then cel.Value = "LOAN31617932" ' etc. etc. Next cel End Sub
如果细胞只有 5
等,那么就这样做
If cel.Value = "6" Then ...
编辑2:但我会听@GarysStudent,添加简单的xlWhole
是你想要的。 简单,干净,简单。