【问题标题】:VBA Format a Number in a loop for conditional formattingVBA在循环中格式化数字以进行条件格式化
【发布时间】:2016-01-10 21:07:49
【问题描述】:

我正在尝试对 Excel 图表上的最大和最小数字进行颜色编码。按照 Peltiertech.com 的想法,我有一个有效的代码。然而问题是 Excel 中的数字被格式化为没有小数点 (FormulaRange4.NumberFormat = "0")。我的 VBA 公式检查的值未格式化。结果,我的“最小值”被读取为 265.875 而不是四舍五入的 266。因此,代码无法找到我的最小值。

有没有人可以解决这个问题?下面是代码。子程序相当大,但关注的部分以“'Sub wiseowltutorial()”开头

Set FormulaRange3 = .Range(.Cells(d, c + 2), .Cells(r - 1, c + 3))
FormulaRange3.NumberFormat = "0"
Set FormulaRange4 = .Range(.Cells(d, c + c + 3), .Cells(r - 1, c + c + 3))
FormulaRange4.NumberFormat = "0"
Set SelectRanges = Union(FormulaRange3, FormulaRange4)

SelectRanges.Select
ActiveSheet.Shapes.AddChart.Select
With ActiveChart
.Type = xlColumn
.HasTitle = True
.ChartTitle.Text = "Individual Employee Productivity"
.ChartTitle.Font.Bold = True
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Text = "Employees"
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Text = "Widgets Produced"
.Axes(xlValue).MajorGridlines.Delete
.ApplyDataLabels
.Legend.Delete
.Parent.Name = "Individual Employee Productivity"

结束

结束于 '结束子

'Sub from YouTubewiseowltutorial() '找到正确的方法来突出每个团队中效率最高和效率最低的人或人

Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppSlide As PowerPoint.Slide
Dim ppTextbox As PowerPoint.Shape
Dim ppiPoint As Long
Dim ppvValues As Variant
Dim pprValue As Range

Dim lMax As Long
lMax = WorksheetFunction.Max(FormulaRange4)
Dim lMin As Long
lMin = WorksheetFunction.Min(FormulaRange4)

With ActiveChart.SeriesCollection(1)
    ppvValues = .Values
    For ppiPoint = 1 To UBound(ppvValues)
            If ppvValues(ppiPoint) = lMax Then
            .Points(ppiPoint).Format.Fill.ForeColor.RGB = RGB(0, 225, 0)
            End If
            If ppvValues(ppiPoint) = lMin Then
            .Points(ppiPoint).Format.Fill.ForeColor.RGB = RGB(225, 0, 0)
            End If

        Next
 End With

谢谢:)

【问题讨论】:

  • 使用条件格式和最小/最大函数?
  • 嗨@KoebmandSTO 我该怎么做?您是否有适合我当前代码的代码示例?一切都需要自动化和动态 - 没有什么可以手动完成。谢谢
  • 在 VBA 中不进行条件格式化。单击条件格式,-> 基于公式,选择您想要的颜色,然后输入条件,如 =IF(ROUND(A1;0)=ROUND(MIN(A1:A100);0);TRUE;FALSE) (修改它以满足您的确切需求)。

标签: vba excel conditional-formatting


【解决方案1】:

尝试使用 Round():

If Round(ppvValues(ppiPoint),0) = Round(lMax,0) Then
...
...
If Round(ppvValues(ppiPoint),0) = Round(lMin,0) Then

【讨论】:

  • 尤里卡!成功了 :) 谢谢 Fadi - 今天我给你 10 业力点
猜你喜欢
  • 1970-01-01
  • 2011-02-23
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多