我有一张包含超过30,000个条目的表格。我对行D和行F感兴趣。他们有3个基于行D和行F的值组合
A B C D E F
10/11/2016 501 General 2 1 1795 to 2395
10/11/2016 502 Public 1 to 2 1 675 to 750
10/11/2016 503 Public 1 to 3 1 1450
预期结果
A B C D E F
10/11/2016 501 General 2 1 1795 to 2395
10/11/2016 502 Public 1 to 2 1 675 to 750
10/11/2016 503 Public 1 1 1450
我想要达到的目标是: 替换"以及之后的任何事情" [D列]没有"到"(范围)[F栏]
我尝试的代码但没有得到预期的结果:
Sub RemoveAfterTo()
Last = Cells(Rows.Count, "D").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "D").Value) Like "*to*" And (Cells(i, "F").Value) <> "*to*"
Then
Cells(i, "D").Replace What:="to*", Replacement:="", LookAt:=xlPart
End If
Next i
End Sub
感谢任何帮助。 提前致谢。
答案 0 :(得分:1)
<>
无法与外卡一起使用。替换条件(Cells(i, "F").Value) <> "*to*"
条件Not (Cells(i, "F").Value Like "*to*")
这会产生(在修正错位的Then
并按照良好做法声明变量之后):
Sub RemoveAfterTo()
Dim Last As Long, i As Long
Last = Cells(Rows.Count, "D").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "D").Value) Like "*to*" And Not (Cells(i, "F").Value Like "*to*") Then
Cells(i, "D").Replace What:="to*", Replacement:="", LookAt:=xlPart
End If
Next i
End Sub
与您的样本数据一致。