如何在将字符串打印到单元格时保留字符串中的尾随空格?

时间:2017-11-22 06:34:38

标签: excel vba excel-vba

当我尝试打印带有尾随空格的字符串时,尾随空格会被excel自动截断。我想保留它们。

样品:

Sub stringspace()

stringg = "23 "
MsgBox Len(stringg) 'here it is 3
Cells(1, 1) = stringg
MsgBox Len(Cells(1, 1)) 'here it is 2

End Sub

2 个答案:

答案 0 :(得分:1)

设置文本格式以防止自动转换:

Sub stringspace()

    stringg = "23 "
    MsgBox Len(stringg) 'here it is 3
    Cells(1, 1).NumberFormat = "@"
    Cells(1, 1) = stringg
    MsgBox Len(Cells(1, 1)) 'here it is 3

End Sub

答案 1 :(得分:0)

Sub StringSpace_Modified()

    Cells(2, 1) = "'23 "  ' Use a single quote at the beginning
    Debug.Print Len(Cells(2, 1)) ' Displays 3 in immediate window

End Sub