实例需求:表格中的多个图表如下图左侧所示,对于表格进行排序时,希望第一列中的图表跟随相应数据。
方法1:
Sub SortTableWithChart()
Dim oSht As Worksheet, RowCnt As Long, ColCnt As Long
Dim arrData, i As Long, oCht As ChartObject
Set oSht = ActiveSheet
With oSht
For Each oCht In .ChartObjects
oCht.Name = oCht.TopLeftCell.Address(0, 0)
Next
RowCnt = .Cells(.Rows.Count, 2).End(xlUp).Row
ColCnt = .Cells(1, .Columns.Count).End(xlToLeft).Column
.Cells(1, ColCnt + 1) = "Index"
With .Cells(2, ColCnt + 1).Resize(RowCnt - 1)
.FormulaR1C1 = "=ROW()"
.Formula = .Value
End With
With .Range("A1", .Cells(RowCnt, ColCnt + 1))
.Sort Key1:=.Columns(3), Order1: