调整工作簿中的所有图表并调整其大小

时间:2016-07-28 17:29:18

标签: excel vba excel-vba

尝试通过多个工作表创建一个VBA子对齐并调整工作簿中的所有图表,我相信我对调整大小有一个大概,但不知道如何将它们与某个列对齐。对于excel来说仍然相当新,这是我到目前为止的代码:

Sub AllChartAlign()
    Dim works As Worksheet
    For Each works In ActiveWorkbook.Worksheets
        For i = 1 To 2 Step 1
            With ActiveSheet.ChartObjects(i)
                .Height = 234
                .Width = 360
                .Left = Range("Q:Q").Left
            End With
        Next i
    Next works
End Sub

这仅适用于活动工作表,但不会遍历所有工作表。

1 个答案:

答案 0 :(得分:1)

您可以使用.Left属性进行列对齐。如果要将它们与“C”列对齐,可以使用:

Sub AllChartAlign()
    Dim works As Worksheet
    Dim Chrt as ChartObject
    For Each works In ActiveWorkbook.Worksheets
        For i = 1 To works.ChartObjects.Count
            Set Chrt = works.ChartObjects (i)
            With Chrt
                .Height = 234
                .Width = 360
                .Left = Range("C:C").Left
            End With
        Next i
    Next works
End Sub

修改

如果您想要将图表从上到下对齐,您可以使用.Top属性并使用以下内容:

Sub AllChartAlign()
    Dim works As Worksheet
    Dim Chrt as ChartObject
    Dim ChrtHeight As Double

    ChrtHeight = 234
    For Each works In ActiveWorkbook.Worksheets
        For i = 1 To works.ChartObjects.Count
            Set Chrt = works.ChartObjects (i)
            With Chrt
                .Height = ChrtHeight
                .Width = 360
                .Top = ChrtHeight * (i - 1)
                .Left = Range("C:C").Left
            End With
        Next i
    Next works
End Sub

编辑2

(代码现在可以用于工作表中的任意数量的图表,而不是考虑总是有2个图表)