根据两行VBA的条件replace一个值

我有一个有超过30,000个条目的表格。 我对D行和F行感兴趣。它们有3行组合,分别是D行和F行

ABCDEF 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 

预期结果

 ABCDEF 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 

我试图达到的是:当没有“to”(范围)[列F]时,replace“到…之后的任何东西”[D列]

我试过的代码,但没有得到所需的结果:

 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 

任何帮助表示赞赏。 提前致谢。

<>不能用于通配符。 replace条件(Cells(i, "F").Value) <> "*to*"

由条件Not (Cells(i, "F").Value Like "*to*")

这产生了(在修正错误的Then并根据良好实践声明variables):

 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 

其中的样本数据按预期工作。