Public Sub MainRun()
'1. 获取数据集,存储到字典中
Set dict = CreateObject("Scripting.dictionary")
Set dictUse = CreateObject("Scripting.dictionary")
Sheets("数据").Select
Range("J3").Select
Range(Selection, Selection.End(xlToRight)).Select
ColumnsNum = Selection.Count
For i = 1 To ColumnsNum
k = Cells(3, i + 9) & "-" & Cells(5, i + 9) & "-" & Cells(6, i + 9) & "-" & Cells(7, i + 9)
v = Cells(8, i + 9)
dict.Add k, v
Next i
'2. 获取Key list
KeyList = dict.keys
startColumns = 10
'3. 遍历Key list进行数据运算
For Each KeyItem In KeyList
shortKey = Left(KeyItem, Len(KeyItem) - 1)
key3Field = Left(shortKey, Len(shortKey) - 1)
If Not dictUse.exists(key3Field) Then
Sheets("控件").Cells(3, startColumns) = key3Field
dictUse.Add key3Field, key3Field
aSuffix = Right(KeyItem, 1)
For i = 5 To 8 Step 1
steps = Sheets("控件").Cells(i, 6)
oneKey = shortKey & steps
anotherSteps = Sheets("控件").Cells(i, 7)
anotherKey = shortKey & anotherSteps
' 保证两个值都存在的情况下进行数据运算
If dict.exists(anotherKey) And dict.exists(oneKey) Then
Debug.Print dict(oneKey) - dict(anotherKey)
Sheets("控件").Cells(i, startColumns) = dict(oneKey) - dict(anotherKey)
End If
Next
startColumns = startColumns + 1
End If
Next
Sheets("控件").Select
End Sub
Set dict = CreateObject("Scripting.dictionary")
dict.Add key,value
dict.keys
dict.exists(key)
dict(key) = newdata
dict.key(key) = newkeyname
dict.remove key
dict.removeall
运行结果:

>>> 如果你觉得我的文章对你有用,不妨 【点赞】 加 【关注】,你的支持是我持续写作的动力,thank you! <<<