简体   繁体   中英

VBA for Dynamic Chart Data with N/A or Dynamic Range

I can define columns and data for required columns and So on for dynamin range

But how do i convert it into complex charts in the excel file under Tab "Chart Data"

Can anyone advise

Sub create_graph()

Dim ws  As Excel.Worksheet
Dim x As Range
Dim y As Range

Set ws = ActiveWorkbook.Worksheets("Chart Data")

Set x = ws.Range("B8:B" & ws.Cells(ws.Rows.Count, "B").End(xlUp).Row)
Set y = ws.Range("C8:B" & ws.Cells(ws.Rows.Count, "C").End(xlUp).Row)

ws.Shapes.AddChart.Select
With ActiveChart
    .ChartType = xlLine
    .SeriesCollection.NewSeries
    .SeriesCollection(1).Values = x
  End With

End Sub

Now there are 2 questions here

  • can i create a chart where i can ignore blanks and N/A so my data can alight better in Chart
  • can you help me to with additional code to create the image chart

Chart image [1]: https://i.stack.imgur.com/4ElYu.jpg

The chart is not so complex. But your data is not laid out optimally. The #N/A in the Y value columns will not plot points, but all the blanks in the date column are messing you up.

Delete all those rows below row 33. They are what is killing the chart.

Now select the range A7:R33, and on the Insert tab of the ribbon, choose Table. This converts the range into a Table, which is a data structure with advanced properties.

Make sure the chart uses the columns of the Table as its data.

When you add new data to the row below the Table, the Table notices and expands to include the added row. Formulas that reference a whole row of the Table will also expand to include the new row. This means the chart will expand to include the new data.

Here's a tutorial I wrote long ago about the magical dynamic nature of Tables: Easy Dynamic Charts Using Lists or Tables .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM