Excel VBA-获取实际单元格';应用条件格式后的s NumberFormat
例如: 我们有一般格式的简单单元格 让我们添加条件格式,将单元格的NumberFormat更改为Excel VBA-获取实际单元格';应用条件格式后的s NumberFormat,excel,vba,conditional-formatting,Excel,Vba,Conditional Formatting,例如: 我们有一般格式的简单单元格 让我们添加条件格式,将单元格的NumberFormat更改为“###0.00”。现在看起来像这样 问题是如何从VBA代码中获取单元格的当前NumberFormat?考虑到我需要实际显示的格式 当我尝试.NumberFormat或.DisplayFormat.NumberFormat-相同的结果=“常规”。 有没有办法获取正确的数字格式-“###0.00” PS我需要它的原因-我正在尝试制作一个VBA宏,它将保存单元格当前格式,但删除所有条件格式计算。正如
“###0.00”
。现在看起来像这样
问题是如何从VBA代码中获取单元格的当前NumberFormat?考虑到我需要实际显示的格式
当我尝试.NumberFormat
或.DisplayFormat.NumberFormat
-相同的结果=“常规”。
有没有办法获取正确的数字格式-“###0.00”
PS我需要它的原因-我正在尝试制作一个VBA宏,它将保存单元格当前格式,但删除所有条件格式计算。正如@Ron Rosefeld和@FaneDuru所指出的那样-我想唯一的解决方案是循环所有格式条件,并获得有效的格式条件。正如所料,这是相当棘手的 幸运的是,我找到了一个函数来确定给定单元格(如果有的话)当前哪个CF处于活动状态—函数ActiveCondition from。我对它进行了修改,并制作了CFNumberFormat函数,它满足了我的要求。代码如下 例如: 还有一个有效的概念-原来
Rng.FormatConditions(n).NumberFormat
总是返回本地数字格式(.NumberFormatLocal)。也就是说,如果需要将此数字格式应用于其他单元格,则需要将其指定为本地数字格式:
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
私有函数GetStrippedValue(CF作为字符串)作为字符串
作为字符串的Dim Temp
如果InStr(1,CF,“=”,vbTextCompare),则
温度=中间(CF,2,透镜(CF)-1)
如果左(温度,1)=“=”则
温度=中间(温度,2)
如果结束
其他的
温度=CF
如果结束
GetStrippedValue=Temp
端函数
私有函数ActiveCondition(Rng作为范围)作为整数
暗Ndx为长
作为格式化条件的Dim FC
变光温度
Dim Temp2作为变体
如果Rng.FormatConditions.Count=0,则
活动条件=0
其他的
对于Ndx=1到Rng.FormatConditions.Count
设置FC=Rng.FormatConditions(Ndx)
选择案例FC.Type
案例xlCellValue
选择Case FC.操作符
案例xlBetween
温度=GetStrippedValue(FC.公式1)
Temp2=GetStrippedValue(FC.公式2)
如果是数字(温度),则
如果CDbl(额定值)>=CDbl(温度)和_
CDbl(额定值)=温度和_
燃烧值CDbl(温度)然后
ActiveCondition=Ndx
退出功能
如果结束
其他的
如果燃烧值>温度,则
ActiveCondition=Ndx
退出功能
如果结束
如果结束
案例质量
温度=GetStrippedValue(FC.公式1)
如果是数字(温度),则
如果CDbl(额定值)=CDbl(温度),则
ActiveCondition=Ndx
退出功能
如果结束
其他的
如果温度=额定值,则
ActiveCondition=Ndx
退出功能
如果结束
如果结束
案例xlGreaterEqual
温度=GetStrippedValue(FC.公式1)
如果是数字(温度),则
如果CDbl(额定值)>=CDbl(温度),则
ActiveCondition=Ndx
退出功能
如果结束
其他的
如果燃烧值>=温度,则
ActiveCondition=Ndx
退出功能
如果结束
如果结束
无外壳
温度=GetStrippedValue(FC.公式1)
如果是数字(温度),则
如果CDbl(燃烧值) 如果CDbl(Rng.Value)可以复制(Excel Office 365,Win10)@Tim Williams:你的意思是在你的安装中DisplayFormat.NumberFormat
返回正确吗?@FaneDuru-不,我的意思是我可以复制OP的观察结果:我得到的“General”似乎意味着你必须循环应用于该单元格的格式条件;找出哪一个被触发;基于此,您可以返回cell.FormatConditions(Index.NumberFormat)和@FaneDuru,这在O365@Ro