【问题标题】:Specify conditional number format when thousands and decimal separator are the same指定千位和小数分隔符相同时的条件数格式
【发布时间】:2021-11-21 06:04:24
【问题描述】:

我的宏在虚拟机上运行,​​出于某种原因,它使用“,”作为千位和小数分隔符。我想避免对该机器上的区域设置有任何依赖。

由宏创建的工作簿有一个复选框,用于显示以千为单位的金额。我已将其实现为设置复选框(“选中”)时应用的条件格式。 FormatCondition.NumberFormat 属性似乎需要本地格式字符串。我不知道当千位和小数分隔符相同时是否可以创建该字符串。

我考虑过不使用格式,而是在勾选复选框时实际对值进行除法和舍入,但是当我计算这些值的总和时,它会在下游产生舍入误差。

以这段代码为例;

Sub TestFormatting()
    Dim aRange As Range
    Dim baseFormats
    Dim condFormat As String
    Dim aNumber As Long
    Dim bIndex As Integer
    Dim cIndex As Integer
    
    baseFormats = Array("General", "###,###,##0")
    condFormat = "0" + Application.International(xlThousandsSeparator)
    aNumber = 123456789
    Application.Workbooks.Add
    With ActiveSheet
        .Cells(1, 1) = "NumberFormat"
        .Cells(2, 1) = "Conditional NumberFormat"
        .Cells(3, 1) = "=TRUE"
        .Cells(4, 1) = "=FALSE"
        For bIndex = 0 To UBound(baseFormats)
            .Cells(1, 2 + bIndex) = baseFormats(bIndex)
            .Cells(2, 2 + bIndex) = condFormat
            .Cells(3, 2 + bIndex) = aNumber
            .Cells(4, 2 + bIndex) = aNumber
            Set aRange = .Range(.Cells(3, 2 + bIndex), .Cells(4, 2 + bIndex))
            aRange.NumberFormat = baseFormats(bIndex)
            Call aRange.FormatConditions.Add(xlExpression, , "=$A3")
            aRange.FormatConditions(1).NumberFormat = condFormat
        Next bIndex
        With .UsedRange.columns
            .ColumnWidth = 30
            .HorizontalAlignment = xlRight
        End With
    End With
End Sub

当使用描述的区域设置(两个分隔符都是“,”)运行时,会生成以下输出:

我希望它生成与我更改为使用句点(“.”)作为小数分隔符时相同的输出:

【问题讨论】:

    标签: excel vba localization


    【解决方案1】:

    我不完全确定它是如何工作的,但是这个代码更新似乎可以解决问题:

    Sub TestFormatting()
        Dim aRange As Range
        Dim baseFormats
        Dim condFormat As String
        Dim aNumber As Long
        Dim bIndex As Integer
        Dim cIndex As Integer
        Dim xstSep As String
        
        baseFormats = Array("General", "###,###,##0")
        aNumber = 123456789
        Application.Workbooks.Add
        With ActiveSheet
            .Cells(1, 1) = "NumberFormat"
            .Cells(2, 1) = "Conditional NumberFormat"
            .Cells(3, 1) = "=TRUE"
            .Cells(4, 1) = "=FALSE"
            For bIndex = 0 To UBound(baseFormats)
                .Cells(1, 2 + bIndex) = baseFormats(bIndex)
                .Cells(3, 2 + bIndex) = aNumber
                .Cells(4, 2 + bIndex) = aNumber
                Set aRange = .Range(.Cells(3, 2 + bIndex), .Cells(4, 2 + bIndex))
                aRange.NumberFormat = baseFormats(bIndex)
                Call aRange.FormatConditions.Add(xlExpression, , "=$A3")
                
                ' >>> Temporarily change decimal separator and evaluate the conditional format string
                xstSep = Application.DecimalSeparator
                Application.DecimalSeparator = "."
                Application.UseSystemSeparators = False
                condFormat = "0" + Application.International(xlThousandsSeparator)  ' Moved here from above
                .Cells(2, 2 + bIndex) = condFormat                                  ' Moved here from above
                aRange.FormatConditions(1).NumberFormat = condFormat
                Application.UseSystemSeparators = True
                Application.DecimalSeparator = xstSep
                ' <<<
                
            Next bIndex
            With .UsedRange.columns
                .ColumnWidth = 30
                .HorizontalAlignment = xlRight
            End With
        End With
    End Sub
    

    它的作用是在应用数字格式的过程中临时将小数分隔符设置为句点(.)。

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 2017-02-07
      • 2013-11-07
      • 1970-01-01
      相关资源
      最近更新 更多