Excel charts - setting series end dynamically

后端 未结 7 1973
暗喜
暗喜 2020-12-10 07:21

I\'ve got a spreadsheet with plenty of graphs in it and one sheet with loads of data feeding those graphs.

I\'ve plotted the data on each graph using



        
7条回答
  •  长情又很酷
    2020-12-10 08:19

    You can set the range for a chart dynamically in Excel. You can use something like the following VBA code to do it:

    Private Sub Worksheet_Change(ByVal Target as Range)
        Select Case Target 
        Case Cells(14, 2)
            Sheet1.ChartObjects(1).Chart.SetSourceData Range("$C5:$C$" & Cells(14,2))
        ...
        End Select
    End Sub
    

    In this case, the cell containing the number of the last row to include is B14 (remember row first when referring to the Cells object). You could also use a variable instead of the Cells reference if you wanted to do this entirely in code. (This works in both 2007 and 2003.) You can assign this procedure to a button and click it to refresh your chart once you update the cell containing the last row.

    However, this may not be precisely what you want to do ... I am not aware of a way to use a formula directly within a chart to specify source data.

    Edit: And as PConroy points out in a comment, you could put this code in the Change event for that worksheet, so that neither a button nor a key combination is necessary to run the code. You can also add code so that it updates each chart only when the matching cell is edited.

    I've updated the example above to reflect this.

提交回复
热议问题