Vba 插入图表并使用不同工作表中的数据

Vba 插入图表并使用不同工作表中的数据,vba,excel,Vba,Excel,我试图在工作表中插入一个图表,它将显示来自4列的信息 x轴上有日期,y轴上有$figure金额。 这是我从录制宏中得到的(至少显示了我想要的内容): 问题是,这只是扑通一个图表在中间的工作表,我期待有它在一个特定的位置 我想有一种更好的方法来写这篇文章,但我对VBA还不熟悉,并且依赖蛮力方法 任何帮助都将不胜感激 好的,你可以把这个清理一下。宏录制器非常棒,因为它可以帮助您查看需要执行的操作,但它只不过是简单地复制击键或鼠标单击,而VBA编程的要点应该是直接处理对象——比模拟击键更有效 我将展示

我试图在工作表中插入一个图表,它将显示来自4列的信息

x轴上有日期,y轴上有$figure金额。 这是我从录制宏中得到的(至少显示了我想要的内容):

问题是,这只是扑通一个图表在中间的工作表,我期待有它在一个特定的位置

我想有一种更好的方法来写这篇文章,但我对VBA还不熟悉,并且依赖蛮力方法


任何帮助都将不胜感激

好的,你可以把这个清理一下。宏录制器非常棒,因为它可以帮助您查看需要执行的操作,但它只不过是简单地复制击键或鼠标单击,而VBA编程的要点应该是直接处理对象——比模拟击键更有效

我将展示一些使用变量使代码更简洁、更易于解释的示例。我会给你评论的,这样你就能知道我在做什么,为什么

Sub Test()
Dim cObj As Shape  'A Shape container for the Chart
Dim cht As Chart   'Variable will represent the chart
Dim srs As Series  'Variable will represent series

    Set cObj = ActiveSheet.Shapes.AddChart   '##cObj will refer to this shape
    Set cht = cObj.Chart                     '## cht will refer to this chart

    '(Now, the variable "cht" will refer to this specific chart until/unless
    '  you assign it to another chart.

    '## Set the chart type:
        cht.ChartType = xlXYScatterSmoothNoMarkers

    '## To manipulate the chart's size and location you can use
    '   something like this to align it with a cell
    '   there are also Height, Top, Left, Width, etc.
        cObj.Top = Range("A1").Top
        cObj.Left = Range("A1").Left

    '## To manipulate it's size, you can work with
    ' there are also Height, Top, Left, etc. properties, etc.
        cObj.Width = 500
        cObj.Height = 300

    '## Manipulating the PlotArea, etc.
    ' there are also Height, Top, Left, etc. properties, etc.
        cht.PlotArea.Width = 450
        cht.PlotArea.Height = 250

    '## Add series to the chart

    Set srs = cht.SeriesCollection.NewSeries
        srs.Name = "=""target"""
        srs.XValues = "=MacroParty!$R$4:$R$55"
        srs.Values = "=MacroParty!$S$4:$S$55"

    '## "srs" will refer to SeriesCollection(1) until/unless you 
    '    assign it to something else...

    '... like now, we want to add another series, 
    '  so we will just assign another NewSeries to the "srs" variable
    ' and work with the variable srs.
    Set srs = cht.SeriesCollection.NewSeries
        srs.Name = "=""current"""
        srs.XValues = "=VA05NDump!$G$2:$G$833"
        srs.Values = "=VA05NDump!$G$2:$G$833"
End Sub

谢谢你,这是非常翔实的。我会将此保存为将来图表使用的参考。@David我在“cht.PlotArea.width=450”行上不断收到一个错误“对象“PlotArea”的方法“width”失败:“我想这是一个已知的错误。实际上,我在PlotArea以及与图表相关的一些其他维度上观察到了它。事实上,我几乎100%确定这是MS的一个错误,而不是代码的问题,因为
PlotArea
是一个属性,而不是图表的一种方法,所以即使错误消息也不正确:)根据我的经验,解决方法是执行
cht.PlotArea。选择
,然后
cht.PlotArea.Width=450
,等。它可能会在
.Height
上引发类似错误。请参阅示例。这是我和其他人都遇到过的问题。解决方案似乎是选择要格式化的项目。
Sub Test()
Dim cObj As Shape  'A Shape container for the Chart
Dim cht As Chart   'Variable will represent the chart
Dim srs As Series  'Variable will represent series

    Set cObj = ActiveSheet.Shapes.AddChart   '##cObj will refer to this shape
    Set cht = cObj.Chart                     '## cht will refer to this chart

    '(Now, the variable "cht" will refer to this specific chart until/unless
    '  you assign it to another chart.

    '## Set the chart type:
        cht.ChartType = xlXYScatterSmoothNoMarkers

    '## To manipulate the chart's size and location you can use
    '   something like this to align it with a cell
    '   there are also Height, Top, Left, Width, etc.
        cObj.Top = Range("A1").Top
        cObj.Left = Range("A1").Left

    '## To manipulate it's size, you can work with
    ' there are also Height, Top, Left, etc. properties, etc.
        cObj.Width = 500
        cObj.Height = 300

    '## Manipulating the PlotArea, etc.
    ' there are also Height, Top, Left, etc. properties, etc.
        cht.PlotArea.Width = 450
        cht.PlotArea.Height = 250

    '## Add series to the chart

    Set srs = cht.SeriesCollection.NewSeries
        srs.Name = "=""target"""
        srs.XValues = "=MacroParty!$R$4:$R$55"
        srs.Values = "=MacroParty!$S$4:$S$55"

    '## "srs" will refer to SeriesCollection(1) until/unless you 
    '    assign it to something else...

    '... like now, we want to add another series, 
    '  so we will just assign another NewSeries to the "srs" variable
    ' and work with the variable srs.
    Set srs = cht.SeriesCollection.NewSeries
        srs.Name = "=""current"""
        srs.XValues = "=VA05NDump!$G$2:$G$833"
        srs.Values = "=VA05NDump!$G$2:$G$833"
End Sub