如果您只需要每个共同贡献者的总数,您可以使用以下 Excel 公式来完成。根据贡献者名称更改 Criteria,例如 JPM、CITG、BAML... 等等。
公式:
=SUMIF($C$2:$C$13,"*JPM*",$B$2:$C$13)
输出
JPM CITG BAML BCG CIBC DB
2432.74 500 500 500 1812.84 1812.84
这对你来说足够了吗?否则你想在分布式透视视图中显示每个贡献者的价格吗?
由于 OP 的要求是按行对每个贡献者的价格进行排序,因此对 VBA 片段进行了编码。为了更容易直观地理解解决方案枢轴显示在您的源数据旁边。
以下是解决和输出的步骤。
- 在您的工作表中添加一个按钮。
- 双击按钮并添加
Call splitSortPivot()
- 将模块添加到您的 Excel 项目
- 双击模块并复制以下代码
- 保存,编译
代码:
Option Explicit
Public Sub splitSortPivot()
Dim ws As Worksheet
Dim sourceRange As Range
Dim rng As Range
Dim i, j, k As Integer
Dim sourceArray As Variant
Dim arrangersArray As Variant
Dim ary As Variant
Set ws = Worksheets("Sheet1")
Set sourceRange = ws.Range("B2:C13")
'Based on the number of records +2 you have in source sheet (Sheet1)
'you may set the first dimension's upper bound of the array
'+1 for header and +1 for total --> in current case 12 + 1 + 1 = 14
'even setting this number can be done programmatically using used rows in C column.
ReDim arrangersArray(0 To 13, 5)
'This code can be optimized to add names programmatically
arrangersArray(0, 0) = "JPM"
arrangersArray(0, 1) = "CITG"
arrangersArray(0, 2) = "BAML"
arrangersArray(0, 3) = "BCG"
arrangersArray(0, 4) = "CIBC"
arrangersArray(0, 5) = "DB"
sourceArray = sourceRange.Value
For j = LBound(sourceArray, 1) To UBound(sourceArray, 1)
If InStr(1, sourceArray(j, 2), ",") > 0 Then
ary = Split(sourceArray(j, 2), ",")
For k = LBound(ary) To UBound(ary)
For i = LBound(arrangersArray, 2) To UBound(arrangersArray, 2)
If arrangersArray(0, i) = Trim(ary(k)) Then
arrangersArray(j, i) = sourceArray(j, 1)
arrangersArray(13, i) = arrangersArray(13, i) + arrangersArray(j, i)
End If
Next i
Next k
Else
For k = LBound(arrangersArray, 2) To UBound(arrangersArray, 2)
If arrangersArray(0, k) = sourceArray(j, 2) Then
arrangersArray(j, k) = sourceArray(j, 1)
arrangersArray(13, k) = arrangersArray(13, k) + arrangersArray(j, k)
End If
Next k
End If
Next j
'Output the processed array into the Sheet.
Range("G1").Resize(UBound(arrangersArray) + 1, _
UBound(Application.Transpose(arrangersArray))) = arrangersArray
End Sub
输出屏幕截图: