简体   繁体   English

根据单元格值更改箭头颜色?

[英]Change arrow color based on cell value?

I'm working on a school project and would like to overlay arrows on top on an image to describe traffic flow. 我正在做一个学校项目,想在图像上方叠加箭头以描述交通流量。 If there's little traffic ("A1" < 20) I'd like the arrow to appear green. 如果交通不畅(“ A1” <20),我希望箭头显示为绿色。 Other wise I'd like the arrow to change to the color red. 否则,我希望箭头更改为红色。 I've tried using the if-then-else statement in my VBA code, but keep getting a compile error that's driving me nuts. 我曾尝试在VBA代码中使用if-then-else语句,但始终遇到使我发疯的编译错误。 This is what I have so far: 这是我到目前为止的内容:

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

you don't specify which compile error you are getting, but as John Bustos mentioned you are missing End With . 您没有指定要获取的编译错误,但是正如John Bustos提到的那样,您缺少End With

Here is an update to your code. 这是您代码的更新。

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

I'm not convinced the other answer works so offer a slight variation: 我不相信其他答案有效,因此请稍作改动:

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

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

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