失去格式的Excel单元格使用VBAreplace

我正在使用带有标签的Excel模板,必须使用VBAreplace为input值。

要做到这一点,我使用了:

Cells.Replace What:="&/TDT/&", Replacement:="123456987654321456654444", _ LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _ True, ReplaceFormat:=False 

但执行此replace后单元格中的显示值为1,23457E + 23。 我试过在单元格设置中使用不同的格式,我试图改变ReplaceFormat和SeachFormat参数而不改变结果。

任何人都知道为什么excel不尊重单元格的格式?

使用的格式是replace单元的文本,Office的版本是2007/2010。 我需要的是在更换后在单元格中看到123456987654321456654444而不是1,23457E + 23。

你可以做的是结合使用Value属性和Find方法来实现安全(或更安全)的replace,如下所示:

 Sub SafeReplace(ByVal What As String, ByVal Replacement As String) Set cell = Cells.Find(What:=What, LookAt:=xlPart, MatchCase:=False, SearchFormat:=False) Do While Not cell Is Nothing cell.Value = Replacement Set cell = Cells.FindNext(cell) Loop End Sub 

然后使用它

 SafeReplace "&/TDT/&", "123456987654321456654444" 

它应该尊重单元格的格式。

'可以在值的前面使用它们作为文本:

 Cells.Replace What:="&/TDT/&", Replacement:="'123456987654321456654444"