在数组中更改NumberFormat(带表达式)

时间:2016-12-07 17:13:28

标签: excel vba number-formatting

我想使用表达式来更改数组的格式编号:

'ArrayTOService(i) = Format(ArrayTOService(i), "[>=1000000] $#,##0.0,,""M"";[>0] $#,##0.0,""K"";General")
ArrayTOService(i).NumberFormat = "[>=1000000] $#,##0.0,,""M"";[>0] $#,##0.0,""K"";General"

我尝试过这两种解决方案。他们都没有工作。

1 个答案:

答案 0 :(得分:0)

如果     ArrayTOService

只是一个VBA数组,您不需要限定符:

Sub dural()
    Dim ArrayTOService(1 To 2) As String
    For i = 1 To 2
        ArrayTOService(i) = "[>=1000000] $#,##0.0,,""M"";[>0] $#,##0.0,""K"";General"
    Next i
End Sub

修改#1:

我们需要将每个数字值转换为字符串

Sub dural2()
    Dim ArrayTOService(1 To 2) As String
    ArrayTOService(1) = 1.123
    ArrayTOService(2) = -8.123

    For i = 1 To 2
        ArrayTOService(i) = Format(ArrayTOService(i), "[>=1000000] $#,##0.0,,""M"";[>0] $#,##0.0,""K"";General")
    Next i

    MsgBox ArrayTOService(1) & vbCrLf & ArrayTOService(2)
End Sub

enter image description here