Excel VBA – 将不同的值映射到实体

在Excel中,我有一个如下所示的表格。 这些人有几个人和几千个工作单。

这是一个演示表格:

在这里输入图像说明

我想要的VBA是一个数据结构,映射每个人的工作量和总收入。 所以我们知道乔纳森做了3份工作,赚了400美元。

(Key) – >(Value,Value)

(人) – >(工作数量,总金额)

我需要上面的数据透视表的值,但不想在VBA中绘制数据透视表只是为了得到这些值。

在这里输入图像说明

所以我的问题是:一个 – 我怎样才能写一个字典或集合,这在VBA中是这样做的,而且两个字典是最有效的方法。 即有更好的方法吗?

假设你可以对数据进行sorting,这对你来说可以正常工作。 testing了超过30,000行的数据,并在不到0.1秒内成功完成。 评论代码的清晰度:

 Sub ParseJobData() Dim ws As Worksheet Dim rData As Range Dim aData As Variant Dim aResults() As Variant Dim sTemp As String Dim ResultIndex As Long Dim i As Long Set ws = ActiveWorkbook.ActiveSheet 'Make sure this is the correct sheet Set rData = ws.Range("F1").CurrentRegion 'Make sure this is the correct range rData.Sort rData.Columns(1), xlAscending, Header:=xlYes 'Sort data aData = rData.Value 'Load data into array ReDim aResults(1 To 65000, 1 To 3) 'Save data in Results array 'Starting at 2 in order to skip header row For i = 2 To UBound(aData, 1) 'Check if this is a new name 'Data is sorted, so new name only happens when previous name is completed If aData(i, 1) <> sTemp Then 'New name, increase ResultIndex, store the name ResultIndex = ResultIndex + 1 sTemp = aData(i, 1) aResults(ResultIndex, 1) = sTemp End If 'Column 2 is a count of jobs, increase it by 1 aResults(ResultIndex, 2) = aResults(ResultIndex, 2) + 1 'Column 3 is a sum of payment, increase by the amount aResults(ResultIndex, 3) = aResults(ResultIndex, 3) + aData(i, 3) Next i 'You have now built your array of unique values with a count and sum 'Do what you want with the array here 'This simply outputs it ws.Range("J1").Resize(ResultIndex, UBound(aResults, 2)).Value = aResults End Sub