Excel打开CSV文件并将小数位数截断为有效位数

时间:2019-01-11 15:33:59

标签: excel csv

我正在打开一个如下所示的CSV文件,

24.0000,19.2500,4.0000,0.7500,

Excel将显示以下仅包含有效数字的截断值。

24 | 19.25 | 4 | 0.75

但是我需要保留小数点后4位(即使无关紧要)。是否可以以某种方式将我的CSV保留为自动文本而不是数字,以便在Excel中显示?即使我尝试在值"4.0000周围加上引号,它仍然被截断。

我尝试了CSV中的这些选项,以使其读取为文本:

4.000[space]

"4.000 "

这些都不起作用。唯一有效的方法是使用非数字, 4.000a

因此Excel将所有数字字段都识别为数字,并强制截断,除非看到非数字字段?

1 个答案:

答案 0 :(得分:1)

尝试

Sub test()
    Dim Wb As Workbook
    Dim Path As String, Fn As String
    Dim Filename As String
    Dim txtFree As Long
    Dim vS As Variant, v As Variant
    Dim vDB(1 To 1000, 1 To 100) As String
    Dim r As Long, c As Integer

    Path = ThisWorkbook.Path & "\"
    Filename = "csvtest.csv" '<~~ your csv filename
    Fn = Path & Filename

    txtFree = FreeFile()
    Open Fn For Input As #txtFree
    Do While Not EOF(txtFree)
        Line Input #txtFree, Fn
        vS = Split(Fn, ",")
        r = r + 1
        c = 0
        For Each v In vS
            c = c + 1
            vDB(r, c) = v
        Next v

    Loop
    Range("a1").Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB
End Sub

结果图片

enter image description here