我沒有注意到你在圖表中工作的事實,但希望以下可以幫助。如果你能得到這些字符,那麼你可以加粗字符串的某些部分。 (假設你的列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)
謝謝,@BruceWayne。所以真的不能使用'SeriesCollection'對象或'LegendEntries'對象來完成。 'legEnt'變量永遠不會返回一個字符串。爲此,我實際上必須創建類似於圖例鍵的形狀,並在其旁邊添加包含信息的文本框。 (我用兩個右對齊百分比曝光列值。)謝謝你嘗試。 –
@DianaTortolini - 很高興你找到了解決方案,聰明的主意。 – BruceWayne