我正在开展一个学校项目,并希望在图像顶部叠加箭头来描述交通流量。如果流量很小(" A1"< 20),我希望箭头显示为绿色。其他明智的我喜欢箭头变成红色。我已经尝试在我的VBA代码中使用if-then-else语句,但不断收到编译错误,这让我很疯狂。这就是我到目前为止所做的:
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
答案 0 :(得分:0)
你没有指定你得到的编译错误,但正如约翰布斯托斯提到你遗漏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
答案 1 :(得分:0)
我不相信其他答案有效,所以提供一点点变化:
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