下面的代碼修改當前圖表的位置和大小以適合覆蓋單元格區(qū)域B2:J18。 Sub FitChartToRange() Dim cht As Chart Dim rng As Range '賦值對象到變量 Set cht = ActiveChart Set rng = ActiveSheet.Range('B2:J18') '移動并調整圖表大小 cht.Parent.Left = rng.Left cht.Parent.Top = rng.Top cht.Parent.Width = rng.Width cht.Parent.Height = rng.Height End Sub
Sub ExportSingleChartAsImage() Dim imagePath As String Dim cht As Chart imagePath = 'C:\完美Excel\myImage.png' Set cht = ActiveChart '導出圖表 cht.Export (imagePath) End Sub 下面的代碼調整當前工作表中所有圖表的大小,以使得它們與當前圖表的大小相同。Sub ResizeAllCharts() Dim chtHeight As Long Dim chtWidth As Long '創(chuàng)建遍歷圖表對象的變量 Dim chtObj As ChartObject '獲取第一個選擇的圖表的大小 chtHeight = ActiveChart.Parent.Height chtWidth = ActiveChart.Parent.Width For Each chtObj In ActiveSheet.ChartObjects chtObj.Height = chtHeight chtObj.Width = chtWidth Next chtObj End Sub
|