我应该在下面的代码范围内做出哪些更改,以便代码搜索每个唯一的合并单元格,并绘制特定部门的合并单元格下面的列的图表。我可以在不同的合并单元格下面拥有动态数量的列(例如,不同的月数)。此外,部门的数量将是动态的。所有这些都建议代码中的其他更改。 (我在代码中使用了' ColumnMax'因为它将用于远程范围,并将用于代替' 7')
请在此处使用示例图表http://bit.ly/1dROvFs查找我的数据。我希望代码动态地为所有部门绘制相同类型的图表。
代码如下:
Sub Charts()
Dim rChart1 As Range
Dim iColumn As Long
Dim lastCol4 As Long
Dim currentrcol As Long
Dim cht1 As Chart
lastCol4 = ActiveSheet.Cells(2, ActiveSheet.Columns.Count).End(xlToLeft).Column
Const StrtRow As Long = 1
Const EndRow As Long = 7
Dim ColumnMax As Long
ColumnMax = lastCol4 - 4
With ActiveSheet
Set rChart1 = .Range(.Cells(StrtRow, "A"), .Cells(EndRow, "A"))
For iColumn = 2 To 7 Step 2
Set rChart1 = Union(rChart1, .Range(.Cells(StrtRow, iColumn), .Cells(EndRow, iColumn)))
Next
Set cht1 = .Shapes.AddChart.Chart
With cht1
.Parent.Left = .Parent.Left - .Parent.Width / 2
.ChartType = xlColumnClustered
.SetSourceData rChart1, xlColumns
End With
End With
End Sub