[英]VBA - get the actual cell's NumberFormat after applying conditional formatting
Example: We have simple cell with General formatting.示例:我们有带有常规格式的简单单元格。
Let's add conditional formatting that will change cell's NumberFormat to "# ##0.00"
.让我们添加条件格式,将单元格的 NumberFormat 更改为
"# ##0.00"
。 Now it looks like this现在看起来像这样
The question is how do I get the current NumberFormat for the cell from VBA code?问题是如何从 VBA 代码中获取单元格的当前 NumberFormat? Given that I need the format that is actually displayed.
鉴于我需要实际显示的格式。
When I try .NumberFormat
or .DisplayFormat.NumberFormat
- same result = "General".当我尝试
.NumberFormat
或.DisplayFormat.NumberFormat
- 相同的结果 = "General"。 Is there a way to get the correct Numberformat - "# ##0.00"
?有没有办法获得正确的 Numberformat -
"# ##0.00"
?
PS The reason why I need it - I'm trying to make a VBA macro that would save cells current formatting but remove all conditional formatting calculations. PS 我需要它的原因 - 我正在尝试制作一个 VBA 宏,它可以保存单元格当前的格式,但删除所有条件格式计算。
As @Ron Rosefeld and @FaneDuru pointed out - guess the only solution is to loop through all the format conditions and get the one that worked.正如@Ron Rosefeld 和@FaneDuru 指出的那样-猜测唯一的解决方案是遍历所有格式条件并获得有效的条件。 As expected it's quite tricky.
正如预期的那样,这非常棘手。
Fortunately I've found a function to determine which CF is currently active for the given cell (if any) - function ActiveCondition from http://www.cpearson.com/excel/cfcolors.htm .幸运的是,我找到了一个函数来确定给定单元格当前哪个 CF 处于活动状态(如果有的话) - 来自http://www.cpearson.com/excel/cfcolors.htm 的函数 ActiveCondition。 I've modified it and made CFNumberFormat function which does what I wanted.
我已经修改了它并制作了 CFNumberFormat 函数,它可以满足我的要求。 The code is below.
代码如下。
Example: https://i.stack.imgur.com/CJyrD.png示例: https : //i.stack.imgur.com/CJyrD.png
One more valid notion - turned out Rng.FormatConditions(n).NumberFormat
always returns Local number format (.NumberFormatLocal).另一个有效的概念 - 结果是
Rng.FormatConditions(n).NumberFormat
总是返回本地数字格式 (.NumberFormatLocal)。 Meaning if you need to apply this NumberFormat to some other cell you'll need to assign it to local number format:这意味着如果您需要将此 NumberFormat 应用于其他某些单元格,则需要将其分配给本地数字格式:
Selection.NumberFormatLocal = Rng.FormatConditions(n).NumberFormat
If you don't then you might get unexpected escaped spaces in number format that will result in errors.如果不这样做,则可能会以数字格式获得意外的转义空格,从而导致错误。
Function code:功能代码:
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.