我没有发现您在图表中工作的事实,但希望以下内容可以提供帮助。如果你能得到字符,那么你可以加粗字符串的某些部分。 (假设你的A列有一个单元格20% Brownies,下一个单元格75% Ice Cream等)
Sub boldPercent()
Dim i&, lastRow&, percentLength&, percentAmt$
Dim k&
lastRow = Cells(Rows.Count, 1).End(xlUp).Row ' Assuming your data is in column A
For i = 1 To lastRow
percentAmt = Left(Cells(i, 1), WorksheetFunction.Search("%", Cells(i, 1)))
percentLength = Len(percentAmt)
With Cells(i, 1).Characters(Start:=1, Length:=percentLength)
.Font.Bold = True
End With
Next i
End Sub
所以也许您可以使用它并对其进行调整以与图表区域一起使用?让 VBA 循环遍历您的图表标题,也许您可以使用上述相同的方法。
编辑:我正在制作一个模拟示例图表来尝试解决这个问题 - 但是您如何将每个类别的百分比放入 Legend 中?我已经设置了一个超级简单的图表,但是不知道你从哪里来(screenshot)
(我希望你的传说会说75% Ice Cream、20% Brownies 等,对吧?)
Edit2:好的,我已经开始使用Chart 对象,希望能够获取每个图例条目,并且会像我上面所做的那样以粗体显示字符......但是,我无法获得legendString永远是一个非空字符串:
Sub Bold_Legend_Text()
Dim stringToFind$
Dim cObj As ChartObject
Dim legEnt As LegendEntry
Dim cht As Chart
Dim i&
Dim percentLength&
Dim legendString$
stringToFind = "%"
For Each cObj In ActiveSheet.ChartObjects
Set cht = cObj.Chart
With cht
If .HasLegend Then
Debug.Print .Legend.LegendEntries.Count
For Each legEnt In .Legend.LegendEntries
' This always returns an empty string, not sure why!
legendString = legEnt.Format.TextFrame2.TextRange.Characters.Text
Debug.Print legendString
' Then we'd find where "%" shows up in the Legend title, and try to bold
' just certain characters
Next legEnt
End If
Next cObj
End Sub
(感谢this thread)