繁体   English   中英

VBA - 应用条件格式后获取实际单元格的 NumberFormat

[英]VBA - get the actual cell's NumberFormat after applying conditional formatting

示例:我们有带有常规格式的简单单元格。

图。1

让我们添加条件格式,将单元格的 NumberFormat 更改为"# ##0.00" 现在看起来像这样

图2

问题是如何从 VBA 代码中获取单元格的当前 NumberFormat? 鉴于我需要实际显示的格式。

当我尝试.NumberFormat.DisplayFormat.NumberFormat - 相同的结果 = "General"。 有没有办法获得正确的 Numberformat - "# ##0.00"

图3

PS 我需要它的原因 - 我正在尝试制作一个 VBA 宏,它可以保存单元格当前的格式,但删除所有条件格式计算。

正如@Ron Rosefeld 和@FaneDuru 指出的那样-猜测唯一的解决方案是遍历所有格式条件并获得有效的条件。 正如预期的那样,这非常棘手。

幸运的是,我找到了一个函数来确定给定单元格当前哪个 CF 处于活动状态(如果有的话) - 来自http://www.cpearson.com/excel/cfcolors.htm 的函数 ActiveCondition。 我已经修改了它并制作了 CFNumberFormat 函数,它可以满足我的要求。 代码如下。

示例: https : //i.stack.imgur.com/CJyrD.png

另一个有效的概念 - 结果是Rng.FormatConditions(n).NumberFormat总是返回本地数字格式 (.NumberFormatLocal)。 这意味着如果您需要将此 NumberFormat 应用于其他某些单元格,则需要将其分配给本地数字格式:

Selection.NumberFormatLocal = Rng.FormatConditions(n).NumberFormat

如果不这样做,则可能会以数字格式获得意外的转义空格,从而导致错误。

功能代码:

    Private Function GetStrippedValue(CF As String) As String
        Dim Temp As String
        If InStr(1, CF, "=", vbTextCompare) Then
           Temp = Mid(CF, 2, Len(CF) - 1)
           If Left(Temp, 1) = "=" Then
               Temp = Mid(Temp, 2)
           End If
        Else
           Temp = CF
        End If
        GetStrippedValue = Temp
    End Function
    
    Private Function ActiveCondition(Rng As Range) As Integer
        Dim Ndx As Long
        Dim FC As FormatCondition
        Dim Temp As Variant
        Dim Temp2 As Variant
        
        If Rng.FormatConditions.Count = 0 Then
            ActiveCondition = 0
        Else
            For Ndx = 1 To Rng.FormatConditions.Count
                Set FC = Rng.FormatConditions(Ndx)
                Select Case FC.Type
                    Case xlCellValue
                    Select Case FC.Operator
                        Case xlBetween
                            Temp = GetStrippedValue(FC.Formula1)
                            Temp2 = GetStrippedValue(FC.Formula2)
                            If IsNumeric(Temp) Then
                               If CDbl(Rng.Value) >= CDbl(Temp) And _
                                   CDbl(Rng.Value) <= CDbl(Temp2) Then
                                   ActiveCondition = Ndx
                                   Exit Function
                               End If
                           Else
                              If Rng.Value >= Temp And _
                                 Rng.Value <= Temp2 Then
                                 ActiveCondition = Ndx
                                 Exit Function
                              End If
                           End If
        
                        Case xlGreater
                            Temp = GetStrippedValue(FC.Formula1)
                            If IsNumeric(Temp) Then
                               If CDbl(Rng.Value) > CDbl(Temp) Then
                                  ActiveCondition = Ndx
                                  Exit Function
                               End If
                            Else
                               If Rng.Value > Temp Then
                                  ActiveCondition = Ndx
                                  Exit Function
                               End If
                            End If
        
                        Case xlEqual
                            Temp = GetStrippedValue(FC.Formula1)
                            If IsNumeric(Temp) Then
                               If CDbl(Rng.Value) = CDbl(Temp) Then
                                   ActiveCondition = Ndx
                                   Exit Function
                               End If
                            Else
                               If Temp = Rng.Value Then
                                  ActiveCondition = Ndx
                                  Exit Function
                               End If
                            End If
        
        
                        Case xlGreaterEqual
                            Temp = GetStrippedValue(FC.Formula1)
                            If IsNumeric(Temp) Then
                               If CDbl(Rng.Value) >= CDbl(Temp) Then
                                   ActiveCondition = Ndx
                                   Exit Function
                               End If
                            Else
                               If Rng.Value >= Temp Then
                                  ActiveCondition = Ndx
                                  Exit Function
                               End If
                            End If
        
                      
                        Case xlLess
                            Temp = GetStrippedValue(FC.Formula1)
                            If IsNumeric(Temp) Then
                                If CDbl(Rng.Value) < CDbl(Temp) Then
                                   ActiveCondition = Ndx
                                   Exit Function
                                End If
                            Else
                                If Rng.Value < Temp Then
                                   ActiveCondition = Ndx
                                   Exit Function
                                End If
                            End If
        
                        Case xlLessEqual
                            Temp = GetStrippedValue(FC.Formula1)
                            If IsNumeric(Temp) Then
                               If CDbl(Rng.Value) <= CDbl(Temp) Then
                                  ActiveCondition = Ndx
                                  Exit Function
                               End If
                            Else
                               If Rng.Value <= Temp Then
                                  ActiveCondition = Ndx
                                  Exit Function
                               End If
                            End If
        
        
                        Case xlNotEqual
                            Temp = GetStrippedValue(FC.Formula1)
                            If IsNumeric(Temp) Then
                               If CDbl(Rng.Value) <> CDbl(Temp) Then
                                  ActiveCondition = Ndx
                                  Exit Function
                               End If
                            Else
                               If Temp <> Rng.Value Then
                                  ActiveCondition = Ndx
                                  Exit Function
                               End If
                            End If
        
                       Case xlNotBetween
                            Temp = GetStrippedValue(FC.Formula1)
                            Temp2 = GetStrippedValue(FC.Formula2)
                            If IsNumeric(Temp) Then
                               If Not (CDbl(Rng.Value) <= CDbl(Temp)) And _
                                  (CDbl(Rng.Value) >= CDbl(Temp2)) Then
                                  ActiveCondition = Ndx
                                  Exit Function
                               End If
                            Else
                               If Not Rng.Value <= Temp And _
                                  Rng.Value >= Temp2 Then
                                  ActiveCondition = Ndx
                                  Exit Function
                               End If
                            End If
                    
                       Case Else
                            Debug.Print "UNKNOWN OPERATOR"
                   End Select
        
        
                Case xlExpression
                    If Application.Evaluate(FC.Formula1) Then
                       ActiveCondition = Ndx
                       Exit Function
                    End If
        
                Case Else
                    Debug.Print "UNKNOWN TYPE"
               End Select
        
            Next Ndx
        
        End If
        
        ActiveCondition = 0
    
    End Function
    
    Private Function CFNumberFormat(Rng As Range) As String
    
        Dim AC As Integer
        AC = ActiveCondition(Rng)
        If AC = 0 Then
            CFNumberFormat = Rng.NumberFormatLocal
        Else
            CFNumberFormat = Rng.FormatConditions(AC).NumberFormat
        End If
    
    End Function


  [1]: https://i.stack.imgur.com/CJyrD.png

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM