绘制具有动态数据行数的折线图

时间:2014-05-30 21:45:20

标签: excel vba graph plot line

我试图根据重量跟踪表上的动态行数在摘要表上绘制折线图(即我每天添加另一行数据)

我试图使我的线图看起来与示例完全相同(相同的颜色等)。现在,我的代码会编译,但图表不会出现在任何地方。

i am trying to make a line graph on the summary sheet based on a dynamic number of lines on the weight tracking sheet (i.e. i add more data every day)

i am trying to make my line graph look exactly like the example (same colors etc.)

以下是电子表格https://drive.google.com/file/d/0B1GLuBx-ROnhSnJpdDRFTFVUbDA/edit?usp=sharing

   Private Sub WeightTrackingChart()

   'variable declaration
    Dim i As Long
    Dim LastRow As Long
    Dim WTchart As Shape
    Dim ws As Worksheet

    'Find the last used row
    LastRow = Sheets("Weight Tracking").Range("B3").End(xlUp).Row

    'Looping from fifth row till last row which has the data
    For i = 6 To LastRow
        'Prints chart to Summary sheet
        Set ws = Sheets("Weight Tracking")

        'deletes old charts
       ' ws.Shapes("WTchart").Delete

        'Adds new chart to the sheet
        Set WTchart = ws.Shapes.AddChart(xlLine, 15, 750, 500, 400)

        'Sets chart name
        WTchart.Name = "WTchart"

        'now the line chart is added...setting its data source here
        With Sheets("Weight Tracking")
            WTchart.SetSourceData Source:=.Range(.Cells(i, 1), .Cells(i, "F"))
        End With


        Next

End Sub

1 个答案:

答案 0 :(得分:1)

首先,我不是使用Cell(x,y)的忠实粉丝,只是这个个人问题,所以你会看到我写下了我感觉舒服但是可以自由调整你先请。看起来您的循环创建了许多图表,而不是向现有图表添加新系列。为此我改变了循环开始的地方。此外,我还激活图表以允许在我的代码中使用ActiveChart。我不知道如何解决这个问题,如果其他人这样做,那将是非常棒的学习。

要注意以后使用的另一件事,请确保正确循环。当你设置循环运行直到你实际上说的最后一行运行7次而不是5次。另外,这应该是数据的长度,而不是循环的时间(要创建的系列)。对于长度,我只是取你的第一行和最后一行之间的差异,并告诉它偏移那么远。

无论如何,我希望这会对你有所帮助。它对我来说非常有效。

Private Sub WeightTrackingChart()

   'variable declaration
    Dim i As Long
    Dim LastRow As Long
    Dim WTchart As Shape
    Dim ws As Worksheet

    'Find the last used row
    LastRow = Sheets("Weight Tracking").Range("B3").End(xlDown).Row
    Length = LastRow - Sheets("Weight Tracking").Range("B3").Row

        'Prints chart to Summary sheet
        Set ws = Sheets("Weight Tracking")

        'deletes old charts
       ' ws.Shapes("WTchart").Delete

        'Adds new chart to the sheet
        Set WTchart = ws.Shapes.AddChart(xlLine, 15, 150, 500, 400)

        'Sets chart name
        WTchart.Name = "WTchart"

        'now the line chart is added...setting its data source here

        With Sheets("Weight Tracking")

            'Looping from fifth row till last row which has the data
            For i = 0 To 4
                WTchart.Select
                ActiveChart.SeriesCollection.NewSeries
                ActiveChart.SeriesCollection(i + 1).Name = .Range("B2").Offset(0, i + 1)
                ActiveChart.SeriesCollection(i + 1).Values = Range(.Range("B2").Offset(1, i + 1), .Range("B2").Offset(Length + 1, i + 1))
                ActiveChart.SeriesCollection(i + 1).XValues = Range(.Range("B3"), .Range("B3").Offset(Length, 0))
            Next
        End With
 End Sub

PS在Google云端硬盘上发布电子表格的主要荣誉让我的生活变得更加轻松。