具有VBA的动态图表

时间:2019-05-02 17:44:39

标签: excel vba charts

我正在编写一个Excel宏,该宏创建具有动态范围的图表(即,每当添加新的一行数据https://trumpexcel.com/wp-content/uploads/2017/08/Dynamic-Chart-Range-in-Excel-Demo.gif时,图表就会自动更新)。对于我正在测试的示例,我的数据在A和B列中。

我确实知道可以通过表格或定义的范围来完成此操作,但不幸的是,这两个项目都不适合该特定项目。

我当前使用的代码(两个不同版本)可以很好地创建图形,而无需我明确定义范围。但是,当我添加新的数据行时,图形不会更新。

Sub AddGraphs()
    'Set the dynamic ranges
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    LC = Cells(1, Columns.Count).End(xlToLeft).Column

    'Create the chart
    Charts.Add
    With ActiveChart
        .ChartType = xlColumnClustered
        .SetSourceData Source:=Range(Cells(1, 1), Cells(lr, LC))
        .Location xlLocationAsObject, "Sheet1"
    End With

    'Format chart and set location
    With ActiveChart
        .Parent.Top = Cells(1, LC + 3).Top
        .Parent.Left = Cells(1, LC + 3).Left
        .HasLegend = False
    End With
End Sub

'Alternative code

Sub Test()
    Dim LastRow As Long
    Dim Rng1 As Range
    Dim rng2 As Range
    Dim ShName As String
    With ActiveSheet
        LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        Set Rng1 = .Range("A2:A" & LastRow & ", B2:B" & LastRow)
        ShName = .Name
    End With
    Charts.Add
    With ActiveChart
        .ChartType = xlLine
        .SetSourceData Source:=Rng1

        .Location Where:=xlLocationAsObject, Name:=ShName
    End With
End Sub

2 个答案:

答案 0 :(得分:0)

谢谢@Cyril的创意!

代码现在更新如下:

Option Explicit 'Excel worksheet change event Range A1 to B50
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A2:B50")) Is Nothing Then
            'Set the dynamic ranges

        Dim lr As Integer
        Dim lc As Integer

        lr = Cells(Rows.Count, 1).End(xlUp).Row
        lc = Cells(1, Columns.Count).End(xlToLeft).Column

    'Create the chart
        Charts.Add
        With ActiveChart
            .ChartType = xlColumnClustered
            .SetSourceData Source:=Range(Cells(1, 1), Cells(lr, lc))
            .Location xlLocationAsObject, "Sheet1"
        End With

    'Format chart and set location
        With ActiveChart
            .Parent.Top = Cells(1, lc + 3).Top
            .Parent.Left = Cells(1, lc + 3).Left
            .HasLegend = False
        End With
    End If
End Sub

这很好用,除了每次宏运行时都会创建一个新图表外,我想更新现有图表-希望对您有所帮助!

答案 1 :(得分:0)

我使用Shapes仅在您的活动表上创建了图表,而不是在图表表上创建。

Option Explicit

Sub AddGraphs()
    Dim lr As Long, lc As Long, ch As ChartObject
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    ActiveSheet.Shapes.AddChart.Name = "Cat" '<--- Used Shapes to keep this on the sheet only
    ActiveSheet.ChartObjects("Cat").Select '<--- Naming the Chart to later call and use in Change_Event
    With ActiveChart
        .ChartType = xlColumnClustered
        .SetSourceData Source:=Range(Cells(1, 1), Cells(lr, lc))
        .Location xlLocationAsObject, "Sheet1"
        .Parent.Top = Cells(1, lc + 3).Top
        .Parent.Left = Cells(1, lc + 3).Left
        .HasLegend = False
    End With
End Sub

现在,您已制作出可以在将来使用的图表。您希望更改事件仅与更新数据系列有关,例如:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Cells(Rows.Count, 2).End(xlUp).Row = Cells(Rows.Count, 1).End(xlUp).Row Then Exit Sub
    ChartObjects("Cat").Select '<--- Using named shape/chart
    With ActiveChart
        Dim lr As Long, lc As Long
        lr = Cells(Rows.Count, 1).End(xlUp).Row
        lc = Cells(1, Columns.Count).End(xlToLeft).Column
        .SetSourceData Source:=Range(Cells(1, 1), Cells(lr, lc))
    End With
End Sub

Edit2:重新发布帖子以修复其他一些问题,以使其正常工作。