|
Sub 区域去重复()
Application.ScreenUpdating = False
Dim ar As Variant
Dim i As Long, r As Long, rs As Long
Dim br()
Dim d As Object
Set d = CreateObject("scripting.dictionary")
With Sheets("原始数据区域")
r = .Cells(Rows.Count, 1).End(xlUp).Row
ReDim br(1 To r)
n = 1
br(n) = 3
For i = 3 To r
If .Cells(i, 1).Interior.ColorIndex <> .Cells(i + 1, 1).Interior.ColorIndex Then
n = n + 2
br(n - 1) = i
br(n) = i + 1
End If
Next i
ReDim brr(1 To n, 1 To 3)
For s = 1 To n - 1 Step 2
m = m + 1
brr(m, 1) = "区域" & m
d.RemoveAll
ks = br(s)
js = br(s + 1)
brr(m, 3) = .Cells(ks, 1).Interior.ColorIndex
ar = .Range(.Cells(ks, 1), .Cells(js, 1))
For i = 1 To UBound(ar)
If ar(i, 1) <> "" Then
If Not d.exists(ar(i, 1)) Then
If brr(m, 2) = "" Then
brr(m, 2) = ar(i, 1)
Else
brr(m, 2) = brr(m, 2) & "," & ar(i, 1)
End If
d(ar(i, 1)) = ""
End If
End If
Next i
Next s
End With
With Sheets("期望的结果数据表2")
.UsedRange.Clear
k = 1
For i = 1 To m
k = k + 1
.Cells(k, 1).Resize(1, 2) = Application.Index(brr, i, 0)
.Cells(k, 1).Resize(1, 2).Interior.ColorIndex = brr(i, 3)
Next i
End With
Application.ScreenUpdating = True
MsgBox "ok!"
End Sub
|
|