我正在为我的同事开发一个工具来自动生成堆积条形图。我需要这个适用于任意大小的数据集,任意数量的Product
和任意数量的Part
。所以,我需要为我的宏自动调整VBA范围,我不知道该怎么做。
需要执行以下操作:
转到“选择数据”和“切换行和列”
将Total
数据值变为No Fill
Total
数据标签显示为“Inside Base”我的问题如下:
Total
数据选择设置为无填充。我应该如何仅为Total
数据点执行此操作?以下是我一直在练习的示例数据集: https://docs.google.com/spreadsheets/d/19n_UH2fJxomur13G_I89WRaqW93KH6Z0FIpUOzsciyE/edit#gid=0
这是我手动创建时创建的宏:
Sub StackedBar1()
'
' StackedBar1 Macro
'
'
Range("G2").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-5]:RC[-1])"
Range("G3").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-5]:RC[-1])"
Range("G4").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-5]:RC[-1])"
Range("A1:G4").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Sheet3'!$A$1:$G$4")
ActiveChart.ChartType = xlColumnStacked
ActiveChart.SetSourceData
ActiveChart.SetElement (msoElementDataLabelCenter)
ActiveChart.SeriesCollection(6).Select
ActiveSheet.ChartObjects("Chart 4").Activate
ActiveChart.SeriesCollection(6).DataLabels.Select
ActiveSheet.ChartObjects("Chart 4").Activate
Selection.Position = xlLabelPositionInsideBase
ActiveSheet.ChartObjects("Chart 4").Activate
ActiveChart.Axes(xlValue).Select
ActiveSheet.ChartObjects("Chart 4").Activate
ActiveChart.Axes(xlValue).MaximumScale = 14
ActiveChart.Axes(xlValue).MaximumScale = 8
ActiveSheet.ChartObjects("Chart 4").Activate
ActiveChart.ChartArea.Select
End Sub
答案 0 :(得分:1)
我没有尝试动态,但看起来应该是这样的:
Dim lastRow As Long
Dim lastColumn As Long
'Define last column and last row in datasheet
lastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
lastColumn = Sheet1.Cells(1, Columns.Count).End(xlToLeft).Column
'define range
ActiveChart.SetSourceData Source:=Sheets("Name").Range(Cells(1,1),Cells(lastRow,lastColumn))
设置标签并使其透明:
ActiveChart.SetElement (msoElementDataLabelInsideBase)
ActiveChart.SeriesCollection(lastColumn-1).Fill.Visible = msoFalse
切换行/列,具体取决于您的需要:
ActiveChart.PlotBy = xlColumns
'Or
ActiveChart.PlotBy = xlRows