删除所有工作表中所有图表中的网格线

时间:2016-09-07 14:52:41

标签: excel vba excel-vba

我正在使用以下代码:

Sub RemoveGridlines()

Dim axs As Axes
Dim ws As Worksheet
Dim objCht As ChartObject

For Each ws In Worksheets
        ws.Activate
        For Each objCht In ws.ChartObjects
            With objCht.Chart
                For Each axs In objCht.Chart
                    .Axes.HasMajorGridlines = False
                    .Axes.HasMinorGridlines = False
                Next axs
            End With
        Next objCht
Next ws

End Sub

作为对象不匹配而挂在For Each axs In objCht.Chart上。我尝试将.axes添加到该行,但无论如何都不会继续。 如何让它循环遍历每个轴,图表和工作表?即使我从后续行中删除.Chart并删除.Axes,我也会Method or data member not found

1 个答案:

答案 0 :(得分:1)

你知道,真正的原因很有趣,但现在代码可以运作了:

Sub RemoveGridlines2()

    Dim axs             As Axis
    Dim ws              As Worksheet
    Dim objCht          As ChartObject
    Dim k               As Chart

    For Each ws In Worksheets
        For Each objCht In ws.ChartObjects
            Set k = Sheets(ws.Name).ChartObjects(objCht.Name).Chart
            For Each axs In k.Axes
                axs.HasMajorGridlines = False
                axs.HasMinorGridlines = False
            Next
            Set k = Nothing
        Next objCht
    Next ws

End Sub

实际上,如果您在“DIM”中的代码中将“AXES”更改为“AXIS”,您可能还会得到一个正常工作的代码:) 好好享受! :d

编辑: 这就是你的代码应该是这样的:

Sub RemoveGridlines()

    Dim axs                 As Axis
    Dim ws                  As Worksheet
    Dim objCht              As ChartObject

    For Each ws In Worksheets
        ws.Activate
        For Each objCht In ws.ChartObjects
            For Each axs In objCht.Chart.Axes
                axs.HasMajorGridlines = False
                axs.HasMinorGridlines = False
            Next axs
        Next objCht
    Next ws

End Sub