如何在excel中更改图表的绘图区域?

问题描述 投票:0回答:2

我在我的excel工作簿中创建了一个新图表,我正在尝试重新设计绘图区域,但我的代码似乎不起作用。有帮助吗?

        GlobVars.Wksht = GlobVars.Wkbk.Sheets(1) 'sets current working sheet as first sheet in wkbook
    GlobVars.xlCharts = GlobVars.Wksht.ChartObjects
    GlobVars.myChart = GlobVars.xlCharts.Add(30, 30, 800, 400) 'changes the starting location and height and width
    GlobVars.chartPage = GlobVars.myChart.Chart

    GlobVars.chartPage = GlobVars.myChart.Chart
    GlobVars.chartPage.ChartType = Excel.XlChartType.xlXYScatter 'changes chart type
    GlobVars.chartPage.PlotArea.Width = 600
    GlobVars.chartPage.PlotArea.Height = 300

最后两行是我尝试使用的行。任何帮助是极大的赞赏。

excel vb.net
2个回答
0
投票

如何使用此概念查找上次使用的行和上次使用的列?

Dim lRow As Long
Dim lCol As Long

    'Find the last non-blank cell in column A(1)
    lRow = Cells(Rows.Count, 1).End(xlUp).Row

    'Find the last non-blank cell in row 1
    lCol = Cells(1, Columns.Count).End(xlToLeft).Column

此外,循环访问对象以查找图表并为所述图表指定名称。

Dim sht As Worksheet
Dim CurrentSheet As Worksheet
Dim cht As ChartObject

Application.ScreenUpdating = False
Application.EnableEvents = False

Set CurrentSheet = ActiveSheet

For Each sht In ActiveWorkbook.Worksheets
  For Each cht In sht.ChartObjects
    cht.Activate

    'Do something with the chart...

  Next cht
Next sht

在识别和选择图表对象后,您几乎肯定会想要对其进行更改。您可以在线查找示例代码,或者只需记录宏并单击需要执行的步骤。使用您在上面的示例中派生的名称更新通用“chart1”,“chart2”,“chart3”等。将录制的宏代码与我在此处发布的代码混合,您就可以开展业务了。


0
投票

我想通了,我没有将我的图表绘图区域设置为我的实际图表对象,所以我得到一个空值错误异常。

这是我的代码位:

Dim chartPage As Excel.Chart
        Dim xlCharts As Excel.ChartObjects
        Dim myChart As Excel.ChartObject
        Dim chartRange As Excel.Range

        GlobVars.Wksht = GlobVars.Wkbk.Sheets(1) 'sets current active sheet
        GlobVars.Wksht.Activate()
        xlCharts = GlobVars.Wksht.ChartObjects
        myChart = xlCharts.Add(30, 30, 800, 600) 'changes chart height, width, and starting corner
        chartPage = myChart.Chart 'assign "chartPage" to myChart (this was my error before)
        chartPage.PlotArea.Width = 500
        chartPage.PlotArea.Height = 600
        chartPage.ChartType = Excel.XlChartType.xlXYScatter 'changes chart type
© www.soinside.com 2019 - 2024. All rights reserved.