放大图表 - VBA Excel 2010

时间:2016-01-28 02:13:10

标签: excel vba excel-vba charts

当选择了下拉菜单选项时,我编写了以下代码来创建图表。问题是我的图表很小,我想编写额外的代码来放大显示给用户的图表。我该怎么做呢?

Function Categorical_Chart_Creation(Category As String, Range_A As String, Range_B As String)

'Creates column chart

Set objSelection = Worksheets("Summary Data").Range(Range_A, Range_B) 'Select area to chart

Dim objChart As Chart

Set objChart = Charts.Add

With objChart

.SetSourceData objSelection

.ChartType = xlColumnStacked

.PlotBy = xlColumns

.Axes(xlCategory).CategoryType = xlCategoryScale

.HasLegend = False

.HasTitle = True

.ChartTitle.Text = Category

.Axes(xlCategory, xlPrimary).HasTitle = True

.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Week Beginning"

.Axes(xlValue, xlPrimary).HasTitle = True

.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Total Hours"

.Name = Category

.Location Where:=xlLocationAsObject, Name:="Charts"

End With

End Function

2 个答案:

答案 0 :(得分:0)

您可以访问图表区域的宽度和高度。

Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1")

Dim cht As ChartObject

Dim c As ChartObject
Set c = ws.ChartObjects(1)
c.Chart.ChartArea.Width = 4
c.Chart.ChartArea.Height= 4

或调整图表ScaleHeight& ScaleWidth

ActiveSheet.Shapes("Chart 1").ScaleWidth 0.6775, msoFalse, msoScaleFromTopLeft
ActiveSheet.Shapes("Chart 1").ScaleHeight 0.7638888889, msoFalse, msoScaleFromTopLeft

注意:
在更高版本的Excel(2010+)中,操作图表大小似乎已从独立图表中删除。 https://social.msdn.microsoft.com/Forums/en-US/ded0b457-b9ba-4633-ac75-4ff36472b40c/resizing-charts-in-chart-sheet-problem-in-different-version-of-excel?forum=isvvba

如果查看工作表上的图表属性。你看

enter image description here

如果你看一个独立的图表。你没有获得尺寸选择。

enter image description here

答案 1 :(得分:0)

修复你的代码......

Function Categorical_Chart_Creation(Category As String, Range_A As String, Range_B As String)
  'Creates column chart

  Dim objSelection As Range
  Dim objChart As Chart

  Set objSelection = Worksheets("Summary Data").Range(Range_A, Range_B) 'Select area to chart

  Set objChart = Worksheets("Charts").Shapes.AddChart.Charts
  With objChart
    .SetSourceData objSelection
    .ChartType = xlColumnStacked
    .PlotBy = xlColumns
    .Axes(xlCategory).CategoryType = xlCategoryScale
    .HasLegend = False
    .HasTitle = True
    .ChartTitle.Text = Category
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Week Beginning"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Total Hours"
    .Name = Category
    With .Parent ' ChartObject (container of embedded chart)
      ' size in points
      .Height = 350
      .Width = 500
    End With
  End With
End Function