VBA: Formatting Multiple Selected Charts

烈酒焚心 提交于 2019-12-23 04:41:04

问题


I am looking to format multiple selected charts on Excel 2010 using VBA. The code below works when only one chart is selected but when multiple charts are selected, I get a "run-time error '91' Object variable or With Block variable not set". Any idea how to run the macro for number of selected charts?

Sub ChartFormat5_Click()


''Adjust chart area

'Size
Selection.Width = 631.9
Selection.Height = 290.1

'Border
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.ObjectThemeColor = msoThemeColorText1
    .ForeColor.TintAndShade = 0
    .ForeColor.Brightness = 0
    .Transparency = 0
    .Weight = 1
    .DashStyle = msoLineSolid
End With

'Font
With Selection.Format.TextFrame2.TextRange.Font
    .Name = "Calibri"
    .Size = 10
    .Fill.Visible = msoTrue
    .Fill.ForeColor.ObjectThemeColor = msoThemeColorText1
    .Fill.ForeColor.TintAndShade = 0
    .Fill.ForeColor.Brightness = 0
    .Fill.Transparency = 0
    .Fill.Solid
End With


End Sub

Thanks!


回答1:


This will process the active chart or all selected charts. The first routine determines what to process (active chart or selected charts) and the second processes each.

Sub FormatCharts()
  Dim obj As Object

  If Not ActiveChart Is Nothing Then
    FormatOneChart ActiveChart
  Else
    For Each obj In Selection
      If TypeName(obj) = "ChartObject" Then
        FormatOneChart obj.Chart
      End If
    Next
  End If
End Sub

Sub FormatOneChart(cht As Chart)
  ' do all your formatting here, based on cht not on ActiveChart
End Sub

Don't select parts of the chart, just fully reference them. Instead of

ActiveChart.ChartArea.Select
With Selection.Format.Line

use this

With cht.ChartArea.Format.Line

etc.

Note: this is a duplicate of VBA: Formatting Multiple Selected Charts (Chart, Plot, Legend, etc.)




回答2:


After some trial-n-error, I figured out how to make it work if you have just one or multiple charts selected. It was straightforward, but this worked when I tested it.

Note that I broke the actual Chart Area formatting into a separate sub.

Sub ChartFormat5_Click()

    Select Case TypeName(Selection)

        Case Is = "ChartArea" `only 1 selected

            FormatChart Selection

        Case Is = "DrawingObjects" 'more than 1 selected

            Dim cht As ChartObject
            For Each cht In Selection
                FormatChart cht.Chart.ChartArea
            Next

    End Select

End Sub

Sub FormatChart(chtArea As ChartArea)

With chtArea

    'size
    .Width = 631.9
    .Height = 290.1

    With .Format
        'Border
        With .Line
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorText1
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = 0
            .Transparency = 0
            .Weight = 1
            .DashStyle = msoLineSolid
        End With

        'Font
        With .TextFrame2.TextRange.Font
            .Name = "Calibri"
            .Size = 10
            With .Fill
                .Visible = msoTrue
                With .ForeColor
                    .ObjectThemeColor = msoThemeColorText1
                    .TintAndShade = 0
                    .Brightness = 0
                End With
                .Transparency = 0
                .Solid
            End With
        End With
    End With
End With

End Sub



回答3:


Hy try:

Sub ChartFormat5_Click_v02()

  For i = 1 To Application.Sheets.Count

    Application.Sheets(i).Activate

    For j = 1 To ActiveSheet.ChartObjects.Count

        ActiveSheet.ChartObjects(j).Activate


        'your code here

    Next j

  Next i

End Sub


来源:https://stackoverflow.com/questions/35066448/vba-formatting-multiple-selected-charts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!