如何设置 Table 数据的范围以用于图表
How to Set Range of Table Data for use with Chart
使用 Excel 2010 我正在尝试使用来自多个不同 table 的部分数据在 Sheet1 上创建图表。在下面的示例中,我只是尝试从一个 table 中提取数据作为开始。我似乎很难在 table.
中设置此数据的范围
我最终将循环遍历每个 table 的大约 8 个不同长度的数据列,大约。 21 table 秒。所以任何帮助自动化这将是伟大的
这是我目前所拥有的...
Sub createChart()
Dim rng As Range
Dim chrt As Object
'Table is named BMS_01_01_Module_01 and the column Name I want data from is Cell 1 Voltage
Set chrt = Sheet1.Shapes.AddChart2
chrt.ChartArea.SetSourceData Source:=activesheetlistobjects("BMS_01_01_Module_01").ListColumns("Cell 1 Voltage").Range
chrt.ChartArea.ChartType = x1Line
End Sub
我假设上面的代码产生了 "Object Required" 错误。我建议将语句 (ChartArea.SetSourceData) 分成多个步骤,这样您可以更好地控制代码和灵活性。
要引用 table 范围,只需创建一个 ListObject 变量和一个 Range 变量,其余部分将在下面通过示例进行解释。
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("BMS_01_01_Module_01")
这样一来,就可以非常轻松地引用 table 的任何部分:
Dim rng As Range
'Reference the whole table, including headers:
Set rng = tbl.Range
'Reference just the table data (no headers):
Set rng = tbl.DataBodyRange
'Reference just the data in a single column:
Set rng = tbl.ListColumns("Cell 1 Voltage").DataBodyRange
'Reference the headers only
Set rng = tbl.HeaderRowRange
因此您的代码应该类似于以下内容:
chrt.ChartArea.SetSourceData Source:=tbl.ListColumns("Cell 1 Voltage").DataBodyRange
试试这个。
Sub createChart()
Dim rng As Range
Dim obj As ChartObject
Dim chrt As Chart
Dim Lst As ListObject
Dim cm As ListColumn
Dim Ws As Worksheet
Set Ws = ActiveSheet
Set Lst = Ws.ListObjects("BMS_01_01_Module_01")
Set cm = Lst.ListColumns("Cell 1 Voltage")
'Set cm = Lst.ListColumns(1)
Set rng = cm.DataBodyRange
'Table is named BMS_01_01_Module_01 and the column Name I want data from is Cell 1 Voltage
Set obj = Sheet1.ChartObjects.Add(200, 200, 400, 400)
With obj.Chart
'chrt.ChartArea.SetSourceData Source:=ActiveSheet.ListObjects("BMS_01_01_Module_01").ListColumns("Cell 1 Voltage").Range
.SetSourceData Source:=rng
.ChartType = xlLine
End With
End Sub
我最终对我的代码进行了一些更改。我只希望有一种方法可以引用 table,而不必先激活 sheet,table 就在前面。注意:图表和表格不在同一个 sheets.
这就是最终适用于单个范围的内容:
Sub createChart()
Dim rng As Range
Dim cht As Object
Dim Ws As Worksheet
Set cht = Sheet1.Shapes.addChart
Worksheets("BMS_01_Module_01").Activate
Set Ws = ActiveSheet
Ws.ListObjects("BMS_01_Module_01").ListColumns("Cell 1 Voltage").Range.Select
cht.Chart.SetSourceData Source:=Selection
cht.Chart.ChartType = xlLine
End Sub
使用 Excel 2010 我正在尝试使用来自多个不同 table 的部分数据在 Sheet1 上创建图表。在下面的示例中,我只是尝试从一个 table 中提取数据作为开始。我似乎很难在 table.
中设置此数据的范围我最终将循环遍历每个 table 的大约 8 个不同长度的数据列,大约。 21 table 秒。所以任何帮助自动化这将是伟大的
这是我目前所拥有的...
Sub createChart()
Dim rng As Range
Dim chrt As Object
'Table is named BMS_01_01_Module_01 and the column Name I want data from is Cell 1 Voltage
Set chrt = Sheet1.Shapes.AddChart2
chrt.ChartArea.SetSourceData Source:=activesheetlistobjects("BMS_01_01_Module_01").ListColumns("Cell 1 Voltage").Range
chrt.ChartArea.ChartType = x1Line
End Sub
我假设上面的代码产生了 "Object Required" 错误。我建议将语句 (ChartArea.SetSourceData) 分成多个步骤,这样您可以更好地控制代码和灵活性。
要引用 table 范围,只需创建一个 ListObject 变量和一个 Range 变量,其余部分将在下面通过示例进行解释。
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("BMS_01_01_Module_01")
这样一来,就可以非常轻松地引用 table 的任何部分:
Dim rng As Range
'Reference the whole table, including headers:
Set rng = tbl.Range
'Reference just the table data (no headers):
Set rng = tbl.DataBodyRange
'Reference just the data in a single column:
Set rng = tbl.ListColumns("Cell 1 Voltage").DataBodyRange
'Reference the headers only
Set rng = tbl.HeaderRowRange
因此您的代码应该类似于以下内容:
chrt.ChartArea.SetSourceData Source:=tbl.ListColumns("Cell 1 Voltage").DataBodyRange
试试这个。
Sub createChart()
Dim rng As Range
Dim obj As ChartObject
Dim chrt As Chart
Dim Lst As ListObject
Dim cm As ListColumn
Dim Ws As Worksheet
Set Ws = ActiveSheet
Set Lst = Ws.ListObjects("BMS_01_01_Module_01")
Set cm = Lst.ListColumns("Cell 1 Voltage")
'Set cm = Lst.ListColumns(1)
Set rng = cm.DataBodyRange
'Table is named BMS_01_01_Module_01 and the column Name I want data from is Cell 1 Voltage
Set obj = Sheet1.ChartObjects.Add(200, 200, 400, 400)
With obj.Chart
'chrt.ChartArea.SetSourceData Source:=ActiveSheet.ListObjects("BMS_01_01_Module_01").ListColumns("Cell 1 Voltage").Range
.SetSourceData Source:=rng
.ChartType = xlLine
End With
End Sub
我最终对我的代码进行了一些更改。我只希望有一种方法可以引用 table,而不必先激活 sheet,table 就在前面。注意:图表和表格不在同一个 sheets.
这就是最终适用于单个范围的内容:
Sub createChart()
Dim rng As Range
Dim cht As Object
Dim Ws As Worksheet
Set cht = Sheet1.Shapes.addChart
Worksheets("BMS_01_Module_01").Activate
Set Ws = ActiveSheet
Ws.ListObjects("BMS_01_Module_01").ListColumns("Cell 1 Voltage").Range.Select
cht.Chart.SetSourceData Source:=Selection
cht.Chart.ChartType = xlLine
End Sub