简体   繁体   English

使用 vba 将 A 列中每个唯一值的 B 列中的所有值相加

[英]Sum all values in column B for each unique value in column A using vba

For some reason I think I am making this harder than it should be, but I am trying to do exactly what the title says.出于某种原因,我认为我让这件事变得比应该做的更难,但我正试图完全按照标题所说的去做。 That is, when a person's name appears in one column I want to start a running count/sum of the values in another column throughout a list.也就是说,当一个人的名字出现在一个列中时,我想开始对整个列表中另一列中的值进行运行计数/总和。 For example, in the screenshot provided I would have gathered the following names and sums/values:例如,在提供的屏幕截图中,我会收集以下名称和总和/值:

  • Mary, 5玛丽,5 岁
  • George, 9乔治,9 岁
  • Nick, 5尼克,5 岁
  • Vani, 1瓦尼,1
  • Wes, 7韦斯,7 岁

I want to create a bar chart with each person and their total but at the moment it's showing each entry individually on the chart (for example George would have 5 separate entries on the bar chart with values 3,1,1,3,1).我想为每个人和他们的总数创建一个条形图,但目前它在图表上单独显示每个条目(例如,乔治将在条形图上有 5 个单独的条目,值为 3、1、1、3、1) . All the names are also within a single Named Range as well if that helps.如果有帮助,所有名称也都在一个命名范围内。

Lastly, I want to do all this through VBA.最后,我想通过 VBA 来完成这一切。 The code now just simply looks like this:现在的代码只是看起来像这样:

ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'SettingsSummary'!$D$3:$D$72,'SettingsSummary'!$E$3:$E$72")
ActiveChart.ChartType = xlColumnStacked

在此处输入图像描述

Please, test the next code.请测试下一个代码。 It assumes that the Names are in column A:A and their values in B:B.它假定名称在 A:A 列中,它们的值在 B:B 中。 It skips the keys "-" and add zero in case of value being "-":它跳过键“-”并在值为“-”的情况下添加零:

Sub UniqueValsInChart()
  Dim shT As Worksheet, ch As Chart, lastRow As Long
  Dim arrY, arrX, i As Long, dict As Object
  
  Set shT = ActiveSheet 'use here the sheet you need
  lastRow = shT.Range("A" & shT.rows.count).End(xlUp).row
  arrX = shT.Range("A4:B" & lastRow).value         'put the range in a array
  Set dict = CreateObject("Scripting.Dictionary")    'needed for the next step
  
  On Error Resume Next
   shT.ChartObjects("MyChartXY").Delete             'in case of running again the sub...
  On Error GoTo 0
  
  For i = 1 To UBound(arrX)
    If arrX(i, 1) <> "-" Then
        If Not dict.Exists(arrX(i, 1)) Then
            dict(arrX(i, 1)) = arrX(i, 2)                    'create the unique keys
        Else
            'add the values for the same key if they are not "-":
            dict(arrX(i, 1)) = dict(arrX(i, 1)) + IIf(arrX(i, 2) = "-", 0, arrX(i, 2))
        End If
    End If
  Next i
  arrX = dict.Keys: arrY = dict.items                   'extract the necessary arrays
  
  Set ch = shT.ChartObjects.Add(left:=shT.Range("E4").left, _
            top:=shT.Range("E4").top, width:=250, height:=250).Chart
  With ch
    .Parent.name = "MyChartXY"
    .ChartType = xlBarClustered
    .HasTitle = True
    .ChartTitle.Text = "Unique values..."
    .SeriesCollection.NewSeries.Values = arrY   'feed it with the array elements
    .SeriesCollection(1).XValues = arrX           'feed it with the array elements
  End With
  MsgBox "Ready..."
End Sub

Please, send some feedback after testing it.请在测试后发送一些反馈。

Sounds to me like you want a Pivot table, not a chart.在我看来,您想要一张 Pivot 表,而不是图表。 A Pivot table with rows=Name and Value=Sum of一个 Pivot 表,其中 rows=Name 和 Value=Sum of

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM