Search code examples
asp.netvb.netexcel-charts

How to group excel range in excel charts?


I'm using Microsoft.office.interop.excel and generated an excel with data enter image description here

I am currently showing this data in the same excel as chart also like this: enter image description here

But I need this data to be grouped like this: enter image description here

How can I modify my code to achieve this?

Current code

Dim chartDeliveryDetailDateRd As Excel.Chart
Dim xlChartsDd As Excel.ChartObjects
Dim myChartDdRd As Excel.ChartObject
Dim chartRangeDdRd As Excel.Range

xlChartsDd = xlWorkSheet3.ChartObjects

myChartDdRd = xlChartsDd.Add(50, 100, 500, 250)

chartDeliveryDetailDateRd = myChartDdRd.Chart

chartRangeDdRd = xlWorkSheet3.Range("a1", "i4")
chartDeliveryDetailDateRd.SetSourceData(Source:=chartRangeDdRd)

chartDeliveryDetailDateRd.ChartType = Excel.XlChartType.xlColumnClustered

Solution

  • I find the solution, it was actually one line of code.

    Added

    chartDeliveryDetailDateRd.PlotBy = Excel.XlRowCol.xlColumns
    

    This will plot the chart by columns and in my case the data gets grouped