gpt4 book ai didi

excel - 查找和替换强制科学记数法格式

转载 作者:行者123 更新时间:2023-12-04 18:18:09 26 4
gpt4 key购买 nike

在使用 VBA 进行查找和替换时,我希望 Excel 停止用等效的科学记数法替换包含 E 的完美字符串。

我尝试通过使用 ReplaceFormat.NumberFormat = "@" 来强制它无济于事。

这是说明我的问题的代码:

Sub testsub()

ActiveSheet.Columns("A:A").NumberFormat = "@"

fnd = "2"
rplc = "39456E10"

Range("A1").Value = fnd
Range("A2").Value = rplc

Application.ReplaceFormat.NumberFormat = "@"

ActiveSheet.Cells.Replace what:=fnd, Replacement:=rplc, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=True
End Sub

这就是结果

enter image description here

最佳答案

好吧,这取决于您在 Range 对象中有多少个单元格。您可以选择 Application.Substitute 方法。

我认为只要您使用显式返回 String 数据类型的方法/函数,就可以了。由于 Substitute 正是这样做的,因此您可以尝试以下操作:

Sub Test
Range("A1:A2").Value = Application.Substitute(Range("A1:A2"), "2", "39456E10")
End Sub

假设您的 Range 已经格式化为 "@" 。如果没有,您可能希望将其包含在代码中。这也适用于更大规模的 2D 数组,例如: A1:Z1000

例如:

enter image description here > enter image description here

例如,我认为这同样适用于 RegEx.Replace 方法和 Replace 函数(不要误认为 Replace 方法)。哪个而不是上面需要迭代。

只是一个有趣的事实,使用 Application.Substitute 我们还可以告诉 VBA 我们想要替换哪个出现的搜索值。例如,仅替换数字 2 的第二次出现:

enter image description here
Range("A1:A4") = Application.Substitute(Range("A1:A4"), "2", "39456E10", 2)

enter image description here

但是,根据@Pᴇʜ 他的 answer ,撇号的存在是有原因的... =)。因此,这将是我个人的偏好(并且应该更快)。

关于excel - 查找和替换强制科学记数法格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59789221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com