【问题标题】:VBA to copy chart line color to many charts in different worksheetsVBA将图表线条颜色复制到不同工作表中的许多图表
【发布时间】:2013-11-01 10:31:53
【问题描述】:

在工作中,我总共有 72 个 Excel 2010 工作簿,每个有 12 个工作表,每个工作表上都有一个图表(我认为这意味着图表没有嵌入?)。我是一名基础程序员,只在 A-Level 学习过 VB。

我需要工作簿中的所有图表(在 12 个单独的工作表上)具有与该工作簿中的第一个图表相同的颜色数据线。
我最初的想法是录制一个我手动更改线条颜色、粗细等的宏,然后查看该宏的代码并在其周围放置某种循环。

经过数小时尝试不同的建议和许多谷歌搜索后,我无法让它发挥作用。

我目前的代码如下:

Sub Macro1()

Dim i As Integer
Dim sht As Worksheet

For i = 1 To ActiveWorkbook.Worksheets.Count
Set sht = ActiveWorkbook.Sheets(i)

ActiveSheet.ChartObjects("Chart 1").Activate

ActiveChart.Legend.Select
ActiveChart.Legend.LegendEntries(1).Select
ActiveChart.SeriesCollection(1).Select
With Selection
    .MarkerStyle = 2
    .MarkerSize = 7
End With
Selection.MarkerStyle = -4168
Selection.Format.Fill.Visible = msoFalse
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.ObjectThemeColor = msoThemeColorAccent1
    .ForeColor.TintAndShade = 0
    .ForeColor.Brightness = 0
End With
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 0, 0)
    .Transparency = 0
End With
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 0, 0)
End With
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 0, 0)
    .Transparency = 0
End With
With Selection.Format.Line
    .Visible = msoTrue
    .Weight = 1.25
End With

ActiveChart.Legend.Select
ActiveChart.Legend.LegendEntries(2).Select
ActiveChart.SeriesCollection(2).Select
With Selection
    .MarkerStyle = 1
    .MarkerSize = 7
End With
Selection.MarkerStyle = -4168
Selection.Format.Fill.Visible = msoFalse
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 0, 0)
End With
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 112, 192)
    .Transparency = 0
End With
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 112, 192)
End With
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 112, 192)
    .Transparency = 0
End With
With Selection.Format.Line
    .Visible = msoTrue
    .Weight = 1.25
End With

ActiveChart.Legend.Select
ActiveChart.Legend.LegendEntries(3).Select
ActiveChart.SeriesCollection(3).Select
With Selection
    .MarkerStyle = 3
    .MarkerSize = 7
End With
Selection.MarkerStyle = -4168
With Selection.Format.Fill
    .Visible = msoTrue
    .ForeColor.ObjectThemeColor = msoThemeColorAccent1
    .ForeColor.TintAndShade = 0
'     .ForeColor.Brightness = 0
    .Solid
End With
With Selection.Format.Fill
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 176, 80)
    .Transparency = 0
    .Solid
End With
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 112, 192)
End With
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 176, 80)
    .Transparency = 0
End With
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 176, 80)
End With
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 176, 80)
    .Transparency = 0
End With
With Selection.Format.Line
    .Visible = msoTrue
    .Weight = 1.25
End With
Selection.Format.Fill.Visible = msoFalse

ActiveChart.Legend.Select
ActiveChart.Legend.LegendEntries(4).Select
ActiveChart.SeriesCollection(4).Select
With Selection
    .MarkerStyle = -4168
    .MarkerSize = 7
End With
Selection.Format.Fill.Visible = msoFalse
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 176, 80)
End With
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(112, 48, 160)
    .Transparency = 0
End With
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(112, 48, 160)
End With
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(112, 48, 160)
    .Transparency = 0
End With
With Selection.Format.Line
    .Visible = msoTrue
    .Weight = 1.25
End With


Next i


End Sub

此代码运行并执行我想要的操作,但仅在您实际在 excel 中打开的工作表上运行,它不会在工作簿中的每个工作表上运行和运行宏。有什么想法吗?

提前致谢

【问题讨论】:

    标签: vba excel charts


    【解决方案1】:

    您可以从一个循环中调用您的Sub Macro1,该循环遍历所有Worksheets

    例如:

    Sub WorksheetLoop()
    
         ' Declare Current as a worksheet object variable.
         Dim Current As Worksheet
    
         ' Loop through all of the worksheets in the active workbook.
         For Each Current In Worksheets
    
            ' Insert your code here.
            ' This line displays the worksheet name in a message box.
            MsgBox Current.Name
         Next
    
    End Sub
    

    然后您可以将Current 工作表传递给您的函数并在该表上运行您的代码。 更多信息见:Macro to Loop Through All Worksheets in a Workbook

    在这种情况下,您可以像这样更改代码:

    Sub Macro1(Byval Current As Worksheet)
    
        Dim i As Integer
        Dim sht As Worksheet
    
        For i = 1 To ActiveWorkbook.Worksheets.Count
        Set sht = Current
    
        sht.ChartObjects("Chart 1").Activate
    
        .....
    
    End Sub
    

    然后像这样创建一个循环:

    Sub WorksheetLoop()
    
         Dim Current As Worksheet
    
         For Each Current In Worksheets
            Call Macro1(Current)
         Next
    
    End Sub
    

    【讨论】:

    • 感谢您的快速回复。代码正在运行并且似乎在循环通过每个工作表,但仍然没有影响其他工作表中的图表,第一个图表出现了所需的更改,但所有其他图表保持不变。
    • 如果您还将 ActiveSheet.ChartObjects("Chart 1").Activate 更改为 sht.ChartObjects("Chart 1").Activate,或者您可以更改活动工作表,例如:Current.Activate
    • 所以我的循环应该调用 Function Macro1 并为每个工作表运行它?尝试将 Function Macro1(Current) 调用到循环中时出现错误。有任何想法吗?谢谢
    • 我在您发布的更新代码中收到错误“对象不支持此属性或方法”。
    • 对不起,我忘记了 Call 这个词,如果你添加它应该可以工作。查看更新的示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多