Excel在数字中显示零没有错误

时间:2019-05-12 17:59:53

标签: excel vba numbers format

在excel VBA中,如何显示以下数字而不显示单元格中的“数字存储为文本”错误?

1.0
1.1
1.2
.
.
1.99
1.100
1.101
1.102
.
.
1.20
1.21
1.22
.
.
etc...

1 个答案:

答案 0 :(得分:1)

只需跟踪需要显示的小数位数即可。例如:

Sub marine()
    Dim s As String, i As Long, a
    s = "1.0,1.1,1.2,1.99,1.100,1.101,1.102,1.20,1.21,1.22"
    arr = Split(s, ",")

    i = 1
    For Each a In arr
        With Cells(i, 1)
            .Value = a
            brr = Split(a, ".")
            .NumberFormat = "0." & Application.Rept("0", Len(brr(1)))
        End With
        i = i + 1
    Next a
End Sub

enter image description here

这是基于一个奇怪的事实,即如果VBA将类似数字的字符串放入单元格中,Excel会将其转换为数字:

Sub demo()
    Dim s As String
    s = "1.230"
    Range("A1").Value = s
End Sub

EDIT#1:

另一方面,如果您想输入看起来像数字的文本,但又避免出现错误标志,那么:

Sub Macro1()
    Application.ErrorCheckingOptions.NumberAsText = False
End Sub