Graph 创建具有优先级的二维叠层柱形图

Graph 创建具有优先级的二维叠层柱形图,graph,excel,excel-formula,vba,Graph,Excel,Excel Formula,Vba,我想做的是创建一个二维堆叠图,其中我的系列的位置表示它们在队列中的位置(位置1-堆叠列的最上面部分是最后一个得到服务的位置,位置2-堆叠列的底部部分是第一个) 我已将数据格式化为如下所示(但如果解决方案需要,可以轻松更改): 任务1任务2任务3 此代码要求图例值和数字值分别位于单独的列中,如下所示。本例中不使用标签“任务1”等 A | 100 | B | 400 | B | 510 B | 200 | A | 200 | A | 300 多亏了这一点,不过有一种更简单的方法可以做到这一点,那

我想做的是创建一个二维堆叠图,其中我的系列的位置表示它们在队列中的位置(位置1-堆叠列的最上面部分是最后一个得到服务的位置,位置2-堆叠列的底部部分是第一个)

我已将数据格式化为如下所示(但如果解决方案需要,可以轻松更改):

任务1任务2任务3
此代码要求图例值和数字值分别位于单独的列中,如下所示。本例中不使用标签“任务1”等

A | 100 | B | 400 | B | 510 
B | 200 | A | 200 | A | 300

多亏了这一点,不过有一种更简单的方法可以做到这一点,那就是简单地排序(或Vlookup/index、match等),然后绘制图形。我要查找的图表将使堆叠列中的颜色改变其位置。然而,以这张图表为例,我想要的是A在堆栈列1中位于顶部,然后在堆栈列2中位于底部。这是对堆叠图形的更好描述吗?要更改所需的颜色,您必须在创建图表后访问每个单独的数据点,并确定是否需要使用不同的颜色。上面文章中的代码可以用作基础,您可以对其进行修改,使其循环遍历图表中的所有数据点,并在需要时进行更改。
'Run this macro from the sheet containing your data, after highlightling the data.
Sub Macro3()

  'The below code assumes that you have already selected
  'the columns containing your data and that the first column,
  'and every 2nd column after that contains your legend keys.
  Dim rng As Range
  Set rng = Selection

  Dim colNum As Integer
  Dim rowNum As Integer
  Dim strLegend As String
  Dim rowStart As Integer
  Dim colStart As Integer
  Dim strSeries As String
  Dim i As Integer
  Dim seriesNum As Integer
  Dim shtName As String

  rowStart = rng.Row
  colStart = rng.Column
  shtName = ActiveSheet.Name & "!"

  'Creates an empty chart...
  ActiveSheet.Shapes.AddChart.Select
  '...of type StackedColumn.
  ActiveChart.ChartType = xlColumnStacked

  seriesNum = 0
  'Select all the cells that match the legend in the first column.
  For rowNum = 0 To rng.Rows.Count - 1
    strLegend = Cells(rowStart + rowNum, colStart).Value
    strSeries = "=" & shtName & Cells(rowStart + rowNum, colStart + 1).Address
    For colNum = 2 To rng.Columns.Count - 1 Step 2
        For i = 0 To rng.Rows.Count - 1
            If Cells(rowStart + i, colStart + colNum).Value = strLegend Then
                strSeries = strSeries & "," & shtName & Cells(rowStart + i, colStart + colNum + 1).Address
                Exit For
            End If
        Next
    Next
    'Create a new series.
    ActiveChart.SeriesCollection.NewSeries
    seriesNum = seriesNum + 1
    'Set the legend.
    ActiveChart.SeriesCollection(seriesNum).Name = strLegend
    'Set the X axis labels to nothing, so the default is used.
    ActiveChart.SeriesCollection(seriesNum).XValues = ""
    'Set the series data.
    ActiveChart.SeriesCollection(seriesNum).Values = strSeries
  Next
  'An extra series gets added automatically???
  'This code removes it.
  If ActiveChart.SeriesCollection.Count > rng.Rows.Count Then
    ActiveChart.SeriesCollection(rng.Rows.Count + 1).Delete
  End If
End Sub
A | 100 | B | 400 | B | 510 
B | 200 | A | 200 | A | 300