替换功能将文本格式转换为数字格式。它是excel bug吗?

时间:2014-03-21 10:49:12

标签: excel vba text replace numbers

我不是excel或谷歌搜索的新手,但这种行为令人惊讶/不合逻辑:

  1. 将列“A”格式化为文本
  2. 将一些数字写入单元格,例如74到A1,A2和A3 - 它们被格式化为文本,它们没有总结,看起来很正常
  3. 去寻找 - 替换74乘75
  4. 在替换75的总结之后,它们表现得像数字,即使单元格仍然被格式化为文本!我不明白为什么?!
  5. 这个“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 = "@"
    

    只有解决方案才有效:

    Selection.Replace What:="74", Replacement:="'75"
    

    但我不希望细胞中有叛逆者......

    有没有其他方法可以替换文本,看起来像单元格中的数字 - 通过另一个文本,看起来像数字? 我觉得花一个小时考虑这些基础知识是假的!

    编辑: 即使使用命名范围的这种方法也会给出75作为数字(单元格格式为文本):

    Selection.Replace What:="74", Replacement:=Range("number_75_in_text_format").Text
    

1 个答案:

答案 0 :(得分:0)

你需要描述你是如何得到这个的。尝试运行其中任何一个:

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

更换后,值仍为文字