replacefunction将文本格式转换为数字格式。 它是Excel的错误?
我不是擅长或谷歌search新手,但这种行为是惊人的/不合逻辑的:
- 格式列“A”到文本
- 在单元格中写入一些数字,例如74到A1,A2和A3–它们被格式化为文本,它们没有总结,看起来没有问题
- 去找 – 用75取代74
- 在取代75的总结之后,即使单元格仍然被格式化为文本,它们的行为仍然像数字一样! 我不明白为什么?!
这个“bug”(?)在VBA中引起了一些麻烦,因为下面的代码不会将数据转换为预期的结果(文本格式的“数字”),我根本无法将其转换为文本格式:
Selection.Replace What:="74", Replacement:="75"
同样的结果,当我尝试任何这些方法:
Selection.Replace What:="74", Replacement:=Format("75", "@") Selection.Replace What:="74", Replacement:=CStr("75") Selection.Replace What:="74", Replacement:="75" Selection.NumberFormat = "@"
唯一的解决scheme是这样的:
Selection.Replace What:="74", Replacement:="'75"
但是我不希望这个牢房里的那个亲属
有没有其他方式如何replace文本,看起来像单元格中的数字 – 通过另一个文本,看起来像数字? 我觉得只是假装花一个小时思考这样的基本知识!
编辑:甚至这种使用命名范围的方法给了我75个数字(单元格格式是文本):
Selection.Replace What:="74", Replacement:=Range("number_75_in_text_format").Text
你需要描述你是如何得到这个的。 尝试运行以下任一项:
Sub BugDemo() With ActiveCell .Clear .NumberFormat = "@" .Value = "101" .Replace What:="0", Replacement:="7" End With End Sub Sub BugDemo2() With ActiveCell .Clear .NumberFormat = "@" .Value = "101" .Replace What:="101", Replacement:="7" End With End Sub
更换后,值仍然为文本