excel - 如何将颜色格式化为图表数据标签的几个字符?
问题描述
我有一个饼图,其中包含类别和百分比作为数据标签。我只想要红色的类别名称而不是百分比。我希望这在 VBA 中完成。它在使用时工作font.bold
。
Sub ex()
Dim Ch As ChartObject
Set Ch = Sheet1.ChartObjects(1)
Dim s As Series
Set s = Ch.Chart.SeriesCollection(1)
Dim p As Point
Set p = s.Points(4)
p.DataLabel.Format.TextFrame2.TextRange.Characters(1, 1).Font.Fill.ForeColor.RGB = rgbRed
End Sub
解决方案
如果您的类别名称大小不同,您可能想尝试这样的事情?
Option Explicit
Sub Sample()
Dim objChart As ChartObject
Set objChart = Sheet1.ChartObjects(1)
Dim Chrt As Chart
Set Chrt = objChart.Chart
Dim objSeries As Series
Set objSeries = Chrt.SeriesCollection(1)
Dim p As Point
'~~> Change this to the releavant point
Set p = objSeries.Points(3)
Dim CatValueLength As Long
With p.DataLabel.Format.TextFrame2.TextRange
.InsertChartField msoChartFieldCategoryName, "", 1
'~~> If separator is newline then change
' ", " to "" & Chr(10) & ""
'~~> Or if it something else then change accordingly
.InsertAfter ", "
'~~> Get the category value, length so that we can insert % values
CatValueLength = Len(p.DataLabel.Text)
'~~> Include the percentage
.InsertChartField msoChartFieldPercentage, "", CatValueLength
'~~> Color the category name
.Characters(1, CatValueLength).Font.Fill.ForeColor.RGB = rgbRed
End With
End Sub
在行动
推荐阅读
- python - 独立合并两列后如何创建新的数据框?
- java - 如何使用 payara 服务器 docker POSTBOOT_COMMANDS 为 XX\:OnOutOfMemoryError 创建 JVM 选项
- r - 如何在 R Markdown 中为每个列加粗每个单元格的最小值
- java - 如何在没有 SDK 的情况下为 webjobs 设置警报
- spring - 我正在尝试从请求控制器获取标头信息并读入 IntegrationFlow
- service - 仅当进入信标区域时,如何启用 altbeacon android library forground 服务?
- javascript - 如何使用 React (ref) 获得元素位置?
- ios - 如何检查服务器信任期间使用的证书透明度
- c# - 如何在 Xamarin.forms 中制作没有标题的选项卡栏
- powershell - Get-UnifiedGroup 和 Get-Teams:如何在 powershell 中制作脚本并将其导出到 csv 文件