0

How do I set the source data of an excel interop chart to several entire rows?

I have a .csv file that is created by my program to display some results that are produced. For the sake of simplicity let's say these results and chart are displayed like this: (which is exactly how I want it to be)

enter image description here

Now the problem I am having is that the number of people is variable. So I really need to access the entire rows data.

Right now, I am doing this:

var range = worksheet.get_range("A1","D3");

xlExcel.ActiveChart.SetSourceData(range);

and this works great if you only have three Persons, but I need to access the entire row of data.

So to restate my question, how can I set the source data of my chart to several entire rows?


I tried looking here but couldn't seem to make that work with rows instead of columns.

Community
  • 1
  • 1
Jordan Carroll
  • 696
  • 2
  • 11
  • 29

4 Answers4

3
var range = worksheet.get_range("A1").CurrentRegion;

xlExcel.ActiveChart.SetSourceData(range);

EDIT: I am assuming that the cells in the data region won't be blank.
To test this,
1) place cursor on cell A1
2) press F5
3) click on "Special"
4) choose "Current Region" as option
5) click "OK"

This will select the cells surrounding A1 which are filled, which I believe is what you are looking for.

The translation of that in VBA code points to CurrentRegion property. I think, that should work.

shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
0

Check Out the option Range.EntireRow I'm not 100% on how to expand that to a single range containing 3 entire rows, but it shouldn't be that difficult to accomplish.

Another thing you can do is scan to get the actual maximum column index you need (this is assuming that there are guaranteed to be no gaps in the names), then use that index as you declare your range.

Add Code

int c = 2;//column b
while(true)
{
    if (String.IsNullOrEmpty(worksheet.GetRange(1,c).Value2))
    {
        c--;
        break;
    }
    c++;
}
Nevyn
  • 2,623
  • 4
  • 18
  • 32
0

Take a column from A to D that you're sure has no empty cells.

Do some loop to find the first empty one in that column and it will be one after the last.

Range Cell = SHeet.Range["A1"]; //or another column you're sure there's no empty data

int LineOffset = 0;

 while (Cell.Offset[LineOffset, 0].Value != "")  //maybe you should cast the left side to string, not sure.
{
 LineOffset++;
}

int LastLine = LineOffset - 1;

Then you can get Range[Sheet.Cells[1,1], Sheet.Cells[LastLine, 4]]

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
0

Out of the box here, but why not transpose the data? Three columns for Name, Height, Weight. Convert this from an ordinary range to a Table.

When any formula, including a chart's SERIES formula references a column of a table, it always references that column, no matter how long the table gets. Add another person (another row) and the chart displays the data with the added person. Remove a few people, and the chart adjusts without leaving blanks at the end.

This is illustrated in my tutorial, Easy Dynamic Charts Using Lists or Tables.

Jon Peltier
  • 5,895
  • 1
  • 27
  • 27