Excel-基于数据表中的值有条件地格式化图表上的.DataLabel属性
我曾尝试使用VBA和添加多个数据系列来实现这一点,但我认为由于我的图表类型,我必须使用VBA。我正在使用此模板:。在数据表中,我添加了一个额外的列“类别”,其中包含特定“里程碑”所属的五个不同类别之一。我已经格式化了图表上的数据标签,使其后面有一个填充颜色,我希望能够根据表中里程碑的类别更改该颜色Excel-基于数据表中的值有条件地格式化图表上的.DataLabel属性,excel,vba,object,charts,formatting,Excel,Vba,Object,Charts,Formatting,我曾尝试使用VBA和添加多个数据系列来实现这一点,但我认为由于我的图表类型,我必须使用VBA。我正在使用此模板:。在数据表中,我添加了一个额外的列“类别”,其中包含特定“里程碑”所属的五个不同类别之一。我已经格式化了图表上的数据标签,使其后面有一个填充颜色,我希望能够根据表中里程碑的类别更改该颜色 非常感谢您的帮助 检查此代码的注释,并根据您的需要对其进行自定义 将其放在保存图表的工作表后面: 如果这有帮助,记得标记答案勾选:嗨,谢谢你的回复。我在最初的研究中确实尝试过这一点;但是,它不适用
非常感谢您的帮助 检查此代码的注释,并根据您的需要对其进行自定义 将其放在保存图表的工作表后面:
如果这有帮助,记得标记答案勾选:嗨,谢谢你的回复。我在最初的研究中确实尝试过这一点;但是,它不适用于此图表类型,因为我无法添加多个系列。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
' Declare object variables
Dim evalTable As ListObject
' Declare other variables
Dim sheetName As String
Dim tableName As String
Dim categoryColumnNumber As Long
Dim chartPointNum As Long
' Customize to fit your needs
sheetName = "Project Timeline"
tableName = "ProjectDetails"
categoryColumnNumber = 6
' Initialize table that holds chart data
Set evalTable = ThisWorkbook.Sheets(sheetName).ListObjects(tableName)
' Check if current cell changed belongs to column where categories are defined
If Not Intersect(Target, evalTable.DataBodyRange.Columns(categoryColumnNumber)) Is Nothing Then
' Get the data label number based on cell's position among table
chartPointNum = Target.Row - evalTable.HeaderRowRange.Row
' Set data label background color based on changed cell and it's position
SetDataLabelColor Target, chartPointNum
End If
End Sub
Public Sub SetDataLabelColor(TargetCell As Range, chartPointNum As Long)
Dim evalChart As ChartObject
Set evalChart = ActiveSheet.ChartObjects("Project Timeline")
With evalChart.Chart.FullSeriesCollection(2).Points(chartPointNum).DataLabel.Format.Fill
.Visible = msoTrue
' Credits: https://stackoverflow.com/a/28058868/1521579
.ForeColor.RGB = RGB((TargetCell.Interior.Color Mod 256), ((TargetCell.Interior.Color \ 256) Mod 256), (TargetCell.Interior.Color \ 65536))
.Transparency = 0
.Solid
End With
End Sub