【问题标题】:Excel chart axis scaleExcel图表轴刻度
【发布时间】:2017-08-09 07:23:12
【问题描述】:

一直在使用此blog 将图表轴链接到单元格值。

Sub ScaleAxes()

    Dim wks As Worksheet
    Set ws = Worksheets("AXIS")
    Set cht = ActiveWorkbook.ChartObjects("ChartName1","ChartName2")

    For Each cht In ActiveWorkbook.ChartObjects
        cht.Activate
        With ActiveChart.Axes(xlCategory, xlPrimary)
            .MaximumScale = ws.Range("$B$12").Value
            .MinimumScale = ws.Range("$B$11").Value
            .MajorUnit = ws.Range("$B$13").Value
        End With
    Next cht

End Sub

我的目标是使用具有轴值的单个工作表来更新不同工作表上的多个图表。大多数示例都在同一个工作表上使用图表。我目前收到错误 438 - 有什么想法吗?

【问题讨论】:

    标签: vba excel charts


    【解决方案1】:

    试试下面的代码,代码里面的解释为cmets:

    Option Explicit
    
    Sub ScaleAxes()
    
        Dim Sht As Worksheet
        Dim ws As Worksheet
        Dim chtObj As ChartObject
        Dim ChtNames
    
        Set ws = Worksheets("AXIS")
        ' you need to get the names of the charts into an array, not ChartObjects array
        ChtNames = Array("ChartName1", "ChartName2")
    
        ' first loop through all worksheet
        For Each Sht In ActiveWorkbook.Worksheets
            ' loop through all ChartObjects in each worksheet
            For Each chtObj In Sht.ChartObjects
                With chtObj
    
                    '=== use the Match function to check if current chart's name is found within the ChtNames array ===
                    If Not IsError(Application.Match(.Name, ChtNames, 0)) Then
                        With .Chart.Axes(xlCategory, xlPrimary)
                            .MaximumScale = ws.Range("B12").Value
                            .MinimumScale = ws.Range("B11").Value
                            .MajorUnit = ws.Range("B13").Value
                        End With
                    End If
                End With
            Next chtObj
        Next Sht
    
    End Sub
    

    【讨论】:

    • 如果是图表,是否需要更改图表对象?
    • @tj123 您在帖子中没有提及有关图表工作表的任何内容,您写的是 “不同工作表上的多个图表” 这是不同的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    相关资源
    最近更新 更多