提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何在Excel中根据单元格值更改形状颜色?

例如,如果A1中的单元格值小于100,则形状颜色为红色,如果A1大于100且小于200,则基于特定单元格值更改形状颜色可能是Excel中的一个有趣任务。形状颜色为黄色,当A1大于200时,形状颜色为绿色,如下图所示。 要根据单元格值更改形状的颜色,本文将为您介绍方法。

doc更改形状颜色1

用VBA代码改变基于单元值的形状颜色


箭头蓝色右泡 用VBA代码改变基于单元值的形状颜色


下面的VBA代码可以帮助您根据单元格值更改形状颜色,请执行以下操作:

1。 右键单击要更改形状颜色的工作表选项卡,然后选择 查看代码 从上下文菜单中,弹出 Microsoft Visual Basic for Applications 窗口,请将以下代码复制并粘贴到空白处 模块 窗口。

VBA代码:根据单元格值更改形状颜色:

Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice 20160704
    If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
    If IsNumeric(Target.Value) Then
        If Target.Value < 100 Then
            ActiveSheet.Shapes("Oval 1").Fill.ForeColor.RGB = vbRed
        ElseIf Target.Value >= 100 And Target.Value < 200 Then
            ActiveSheet.Shapes("Oval 1").Fill.ForeColor.RGB = vbYellow
        Else
            ActiveSheet.Shapes("Oval 1").Fill.ForeColor.RGB = vbGreen
        End If
    End If
End Sub

doc更改形状颜色2

2。 然后,当您在单元格A1中输入值时,形状颜色将根据您定义的单元格值进行更改。

注意:在上面的代码中, A1 是你的形状颜色将根据和改变的单元格值 椭圆形1 是插入形状的形状名称,您可以将其更改为您的需要。


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Mark · 10 months ago
    Hi... excellent solution... but how do I apply it to multiple shapes based on the corresponding values of a range of cells. Many thanks in advance for your help.
  • To post as a guest, your comment is unpublished.
    Cesare · 11 months ago
    How do I make the private sub to read the result from the AVERAGE(C1,C5,C9) calculation?

    Sub only works with numeric values; any thoughts and suggestions are greatly appreciated.
  • To post as a guest, your comment is unpublished.
    Steve A · 1 years ago
    Thanks for this which is really useful.

    I now want to use it with a pivot table on another worksheet which controls the data on the sheet with the shapes that I want to change colour. However, when I change the selection on the pivot table the data on the worksheet with the shapes is updated but the code does not run so the shapes do not change colour

    If I manually change the values the code runs and the colour of the shapes is updated.

    Question: what do i need to add to the code above to allow it to run automatically?
  • To post as a guest, your comment is unpublished.
    Yasir · 1 years ago
    How can this be applied if you have multiple shape in the same worksheet?
  • To post as a guest, your comment is unpublished.
    Alan · 2 years ago
    Great vba solution.

    It is possible to also use conditional formatting to colour the shapes.

    Set the name of each shape as the cell value. Using a With Each Shape then set the shape colour as the cell colour for all named shapes.

    The cell colour may be changed using conditional formatting based on numerical values.

    For example the colour of a semi transparent overlap on a city map can be used to graphically indicate population density per block with a graduated colour scheme.
    • To post as a guest, your comment is unpublished.
      Ed · 1 years ago
      Can you share an example of the code?
  • To post as a guest, your comment is unpublished.
    Aleen Khan · 2 years ago
    I have 300 shapes in a sheet. Is it possible to check the adjacent or linked cell's value (empty or non-empty) in a sheet and color the linked shapes through VBA code?
  • To post as a guest, your comment is unpublished.
    Edward · 2 years ago
    How about if we have more than 1 object in the worksheet which the colors change according to the value input say in A1, B1,C1....