|
楼主 |
发表于 2024-3-17 13:35
|
显示全部楼层
- Sub Demo()
- Columns("A:H").Interior.Pattern = xlNone
- Range("C1").Select
- Dim oDic As Object, oDicRng As Object, rngData As Range
- Dim i As Long, sKey As String, arrData
- Set oDic = CreateObject("Scripting.Dictionary")
- Set oDicRng = CreateObject("Scripting.Dictionary")
- Set rngData = Range("G1").CurrentRegion
- arrData = rngData.Value
- For i = LBound(arrData) + 1 To UBound(arrData)
- sKey = arrData(i, 1)
- If oDic.exists(sKey) Then
- oDic(sKey) = oDic(sKey) + arrData(i, 2)
- Set oDicRng(sKey) = Application.Union(oDicRng(sKey), Cells(i, "G").Resize(1, 2))
- Else
- oDic(sKey) = Val(arrData(i, 2))
- Set oDicRng(sKey) = Cells(i, "G").Resize(1, 2)
- End If
- Next i
- Dim lastRow As Long, osht As Worksheet, ic As Long
- ActiveSheet.Cells.Interior.Color = xlNone
- lastRow = Cells(Rows.Count, "A").End(xlUp).Row
- ic = 2 '请自行调整填充颜色
- For i = 2 To lastRow
- sKey = Cells(i, 1)
- If Len(sKey) > 0 Then
- If Val(Cells(i, 2)) = Val(oDic(sKey)) Then
- Set oDicRng(sKey) = Application.Union(oDicRng(sKey), Cells(i, "A").Resize(1, 2))
- ic = ic + 1
- oDicRng(sKey).Interior.ColorIndex = ic
- End If
- End If
- Next
- End Sub
复制代码
运行结果不正确啊! |
-
|