我已经有了这段代码
Excel::_ApplicationPtr app("Excel.Application");
app->Visible[0] = false;
Excel::_WorkbookPtr book = app->Workbooks->Add();
Excel::_WorksheetPtr sheet = book->Worksheets->Item[1];
RangePtr pRange = sheet->Cells;
RangePtr pCell;
pCell = pRange->Item[1][1]; // A1
pCell->Value2 = "1";
pCell = pRange->Item[1][2]; // B1
pCell->Value2 = "1";
pCell = pRange->Item[1][3]; // C1
pCell->Value2 = "10";
pCell = pRange->Item[2][1]; // A2
pCell->Value2 = "3";
pCell = pRange->Item[2][2]; // B2
Cell->Value2 = "1";
pCell = pRange->Item[2][3]; // C2
pCell->Value2 = "20";
和下一个
Excel::RangePtr pBeginRange = pRange->Item[1][1];
Excel::RangePtr pEndRange = pRange->Item[5][9];
Excel::RangePtr pTotalRange = sheet->Range[(Excel::Range *)pBeginRange][(Excel::Range *)pEndRange];
_ChartPtr pChart2 = book->Charts->Add();
pChart2->ChartType = xlBubble3DEffect;
pChart2->SetSourceData((Excel::Range *)pTotalRange, (long)Excel::xlColumns);
如何在Excel图表中禁用自动创建SeriesCollection。我想手动设置范围。在自动创建中,所有SeriesCollection在第一列中都有XValues。但我不需要它。
答案 0 :(得分:2)
我也遇到过同样的问题。从实验中我认为发生了以下情况:
使用以下代码在VBA中创建新图表时
Dim chSheet As Chart
Set chSheet = Charts.Add
Excel,尝试提供帮助我敢打赌,当您从开发人员窗口执行代码时,会自动查看光标选择的工作表,并搜索它认为可能是图表值范围的最近数据集。然后在图表中自动填充。到目前为止,我发现的唯一方法是执行以下代码,在创建它之后立即删除图表上系列集合对象中的所有系列对象。触摸计数器直观,但它的工作原理......
Public Sub DeleteChartSeries(chartSheet As Chart)
'Shorter but perhaps less clean way of writing the code compared to below
Do Until chartSheet.SeriesCollection.Count = 0
chartSheet.SeriesCollection(1).Delete
Loop
'With chartSheet
' Do Until .seriesCollection.Count = 0
' .seriesCollection(1).Delete
' Loop
'End With
End Sub
答案 1 :(得分:1)
只需找到一种解决方法:首先将光标移动到UsedRange
之外的某处,然后创建图表:不会执行自动检测。然后回去。
换句话说,以下代码适用于我(Excel 2007):
' Assume src As Range (proposed source data for the chart) in the ActiveSheet.
' put the cursor somewhere outside the UsedRange to avoid Excel's
' 'helpful' auto detection.
Dim ur As Range
Set ur = src.Worksheet.UsedRange
ur.Cells(1, ur.Columns.Count).Offset(0, 2).Select
Dim crt As Chart
Set crt = src.Worksheet.Shapes.AddChart().Chart
' ^ does NOT do auto-detection.
答案 2 :(得分:0)
可能无法阻止它被创建,但您可以在创建图表后立即将其删除,然后根据需要创建自己的系列。
在VBA中:
Set Chrt = wb.Charts.Add()
Chrt.SeriesCollection(1).Delete