【问题标题】:Change arrow color based on cell value?根据单元格值更改箭头颜色?
【发布时间】:2015-01-24 02:36:09
【问题描述】:

我正在做一个学校项目,想在图片顶部叠加箭头来描述交通流量。如果流量很少(“A1”

Sub DetermineArrowColor()
Dim ncars As Double

Range("A1").Value = ncars
   If ncars < 20 Then
  'change arrow color to green
  ActiveSheet.Shapes.Range(Array("Down Arrow 1")).Select
   With Selection.ShapeRange.Fill
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 176, 80) 
    .Transparency = 0
    .Solid
Else
  'change arrow color to something else
  ActiveSheet.Shapes.Range(Array("Down Arrow 1")).Select
   With Selection.ShapeRange.Fill
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 30, 30) 'whatever the numbers are for the color red
    .Transparency = 0
    .Solid

End If

End Sub

【问题讨论】:

  • 你能指定你在哪里得到编译错误,是什么编译错误?
  • 我没有仔细看,但我发现你错过了 End With 声明......
  • 我不是这方面的专家,但我似乎记得有一个叫做 conditional formatting 的东西可以让你根据它的价值给东西上色 - 试试谷歌搜索吧。
  • @JohnBus​​tos 不错,这显然是答案。

标签: excel if-statement colors vba


【解决方案1】:

您没有指定您遇到的编译错误,但正如 John Bustos 所说,您缺少 End With

这是对您的代码的更新。

Sub DetermineArrowColor()
    Dim ncars As Double

    ncars = Range("A1").Value
    With ActiveSheet.Shapes.Range(Array("Down Arrow 1")).ShapeRange.Fill
        If ncars < 20 Then
        'change arrow color to green
            .Visible = msoTrue
            .ForeColor.RGB = RGB(0, 176, 80)
            .Transparency = 0
            .Solid
        Else
            'change arrow color to something else
            .Visible = msoTrue
            .ForeColor.RGB = RGB(0, 30, 30)    'whatever the numbers are for the color red
            .Transparency = 0
            .Solid
        End If
    End With
End Sub

【讨论】:

  • 他还定义了ncars向后
  • @Chrismas007 不错。我不知道 OP 在那里做什么。
【解决方案2】:

我不相信其他答案有效,所以提供一些细微的变化:

Sub ArrowColour()
Dim ncars As Integer
ncars = Range("A1").Value
    With ActiveSheet.Shapes.Range(Array("Down Arrow 1")).Fill
        If ncars < 20 Then
        .ForeColor.RGB = RGB(0, 176, 80)
        Else
        .ForeColor.RGB = RGB(255, 0, 0)
        End If
    End With
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-29
    • 2017-08-23
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多