[英]Update color of shape based on cell with calculated value
Scenario: I have a dashboard with tiles for things such as "Overdue Tasks". 场景:我有一个显示板,其中包含用于“过期任务”之类的图块。 I have a shape called
tileOverdueTasks
. 我有一个名为
tileOverdueTasks
的形状。 On top of it I have a wordart with a value of =Z11
. 最重要的是,我有一个==
=Z11
的艺术字。 Cell Z11 is calculated based on values in a separate sheet ( Sheet5 (Tasks)
). 单元格Z11是基于单独的工作表(
Sheet5 (Tasks)
)中的值计算的。 If Tasks.Z11 = 0
I want tileOverdueTasks
to be green. 如果
Tasks.Z11 = 0
我希望tileOverdueTasks
为绿色。 If it's anything else, I want it to be red. 如果还有其他,我希望它是红色的。
I found the code below but it does not seem to trigger, I'm assuming due to the fact that cell Z11 is not being changed manually and thus neither is the ActiveWorkbook
. 我发现下面的代码,但它似乎没有触发,我假设由于Z11单元不是手动更改的事实,因此
ActiveWorkbook
也不是。 So, how can I modify this to work given the scenario above? 因此,在上述情况下,我如何修改它才能正常工作?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("Z11")) Is Nothing Then
If IsNumeric(Target.Value) Then
If Target.Value > 0 Then
ActiveSheet.Shapes("tileOverdueTasks").Fill.BackColor.RGB = vbRed
Else
ActiveSheet.Shapes("tileOverdueTasks").Fill.BackColor.RGB = vbGreen
End If
End If
End If
End Sub
UPDATE: I have changed the event to Worksheet.calculate
as suggested by @Scott Craner. 更新:我已将事件更改为
Worksheet.calculate
如@Scott Craner所建议。 Now the event fires and it even enters the proper condition, however the color is not changing. 现在事件触发,它甚至进入了适当的状态,但是颜色没有改变。
There were two problems. 有两个问题。
1) As pointed out by Scott Craner , I needed to use the Worksheet_Calculate
event. 1)正如Scott Craner指出的那样,我需要使用
Worksheet_Calculate
事件。
2) Due to the style of the shape, Fill.BackColor
was not working. 2)由于形状的样式,
Fill.BackColor
不起作用。 I changed it to Fill.ForeColor
and now it works. 我将其更改为
Fill.ForeColor
,现在可以使用了。
Here is the working code: 这是工作代码:
Private Sub Worksheet_Calculate()
If Sheets("Dashboard").Range("Z11").Value > 0 Then
Sheets("Dashboard").Shapes("tileOverdueTasks").Fill.ForeColor.RGB = RGB(185, 0, 0)
Else
Sheets("Dashboard").Shapes("tileOverdueTasks").Fill.ForeColor.RGB = RGB(0, 185, 0)
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.