【问题标题】:Partial font style modification部分字体样式修改
【发布时间】:2016-03-08 10:45:26
【问题描述】:

我正在使用 Excel 2003 中的饼图和图例。

图例条目由如下字符串组成:

75% Ice Cream

20% Brownies

5% Gummy Bears

我试图以粗体显示曝光百分比,但将系列名称的其余部分(冰淇淋、布朗尼或小熊软糖)保留为常规字体。

可以这样做吗?

到目前为止,我一直在处理此代码的变体。此外,我尝试在 SeriesCollection 对象上使用 Split() 函数,甚至录制宏来查看 Excel 在 VBA 中会生成什么。到目前为止,我只能让文本以全部粗体或全部常规字体显示,而不是两者的混合。

For x = 1 To 3
  myChartObject.Chart.Legend.LegendEntries(x).Font.Bold = True
Next x

建议会有所帮助。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    我没有发现您在图表中工作的事实,希望以下内容可以提供帮助。如果你能得到字符,那么你可以加粗字符串的某些部分。 (假设你的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 Cream20% 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 - 很高兴你找到了适合你的解决方案,好主意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多