【问题标题】:Excel VBA Formatting Chart Series with VlookupExcel VBA 格式化图表系列与 Vlookup
【发布时间】:2017-07-18 12:49:10
【问题描述】:

我是新的 VBA 用户,正在尝试根据系列名称格式化图表系列。我想使用 vlookup 来引用系列名称并返回定义的 MarkerStyle、MarkerForegroundColor、MarkerSize 等。我想这样做,因为我有一个大的系列名称列表,每个都需要一个唯一的标记,我需要制作大量图表。我已经启动了一个代码,但一直收到 1004 错误:'无法获取 WorksheetFunction 类的 Vlookup 属性':

 Sub ChartFormattingVlookup()
    Dim mySeries As Series
    Dim cht As ChartObject
    Dim vbc As Range
    Set vbc = Worksheets("VBAChartFormat").Range("A2:I44")

    For Each cht In ActiveSheet.ChartObjects
        cht.Activate
        For Each mySeries In ActiveChart.SeriesCollection
            With mySeries
                .MarkerSize = Application.WorksheetFunction.VLookup(mySeries, vbc, 7, False)
                .MarkerStyle = Application.WorksheetFunction.VLookup(mySeries, vbc, 6, False)
            End With
        Next mySeries
    Next cht
End Sub

如果有任何帮助或建议,我将不胜感激。谢谢!

【问题讨论】:

  • vlookup 的第一个参数中的mySeries 位置不正确。该参数需要一个范围引用或文字字符串,并且您正在传递一个 seriescollection 对象。如果你通过mySeries.Name 它将起作用,只要查找表具有系列名称

标签: excel charts vlookup vba


【解决方案1】:

跟进@Scott Holtzman 评论为什么你在VLookup 函数上收到错误,也不需要Activate cht 然后使用ActiveChart,只需使用For Each mySeries In cht.SeriesCollection

此外,您应该考虑为Application.WorksheetFunction.VLookup 的可能失败添加错误处理。

Option Explicit

Sub ChartFormattingVlookup()

    Dim mySeries As Series
    Dim cht As ChartObject
    Dim vbc As Range

    Set vbc = Worksheets("VBAChartFormat").Range("A2:I44")

    For Each cht In ActiveSheet.ChartObjects
        For Each mySeries In cht.SeriesCollection
            With mySeries
                If Not IsError(Application.VLookup(mySeries.Name, vbc, 7, False)) Then '<-- check VLookup was successfull
                    .MarkerSize = Application.VLookup(mySeries.Name, vbc, 7, False)
                End If
                If Not IsError(Application.VLookup(mySeries.Name, vbc, 6, False)) Then '<-- check VLookup was successfull
                    .MarkerSize = Application.VLookup(mySeries.Name, vbc, 6, False)
                End If
            End With
        Next mySeries
    Next cht

End Sub

【讨论】:

  • 这太好了,非常感谢您的 cmets。 Scott,如果我的系列名称不是字符串而是整数怎么办?
  • 抱歉回复晚了。你说得对,很好,谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-05-04
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多