我有一个范围引用excel中的表.我需要创建一个图表,因此使用我的表格范围设置源数据.我希望源数据是我表中的第一行和最后两行(标题和总共2行).假设范围是使用变量形成的,有没有办法选择任何给定范围的那些部分(假设范围包含三行或更多行).到目前为止,这是我的代码:
Sub addchart(ByVal TableRange As Range, SheetName As Worksheet, TblLabel As String, TableLabel As String)
Dim ChtPosition As Range
Dim ChtRow As Long
Dim ChtSourceData As Range
Set ChtSourceData = Union(Top row of TableRange here, Last 2 rows of TableRange here)
ChtRow = SheetName.Cells(SheetName.Rows.Count, "B").End(xlUp).Row + 2
ChtPosition = SheetName.Cells(ChtRow, 2)
SheetName.Shapes.addchart.Select
With ActiveChart
.SetSourceData Source:=SheetName.Range(ChtSourceData)
.ApplyChartTemplate ("\\graphtemplatefilepath")
.Parent.Name = "Cht" & TblLabel
End With
With SheetName.ChartObjects("Cht" & TblLabel)
.Width = (16 * 29)
.Height = (7 * 29)
.Left …Run Code Online (Sandbox Code Playgroud)