|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
代码如下。。。。
Sub test()
Dim wb As Workbook, sht As Worksheet
Set wb = ThisWorkbook
Set sht = wb.Sheets("sheet1") 'wb.Sheets("sheet2")
For i = 3 To 9
r = sht.Cells(Rows.Count, i).End(3).Row
x = Application.Max(r, x)
Next
arr = sht.Range("c1:i" & x)
ReDim brr(1 To UBound(arr) - 1, 1 To 3)
ReDim drr(1 To UBound(arr) - 1, 1 To 1)
For i = 2 To UBound(arr)
x = Application.Sum(Application.Index(arr, i)) _
- Application.Sum(Application.Index(arr, i - 1))
Select Case Abs(x)
Case 0
brr(i - 1, 1) = "重"
drr(i - 1, 1) = "重"
Case 1
brr(i - 1, 2) = "邻"
drr(i - 1, 1) = "邻"
Case Else
brr(i - 1, 3) = "孤"
drr(i - 1, 1) = "孤"
End Select
Next
ReDim crr(1 To UBound(drr), 1 To 3)
For i = 1 To UBound(drr) - 1
n = 1
For j = i + 1 To UBound(drr)
If drr(j, 1) = drr(i, 1) And j <> UBound(drr) Then
n = n + 1
ElseIf drr(j, 1) <> drr(i, 1) And j <> UBound(drr) Then 'Or j = UBound(drr)
If drr(i, 1) = "重" Then
m = m + 1
i = j - 1: crr(m, 1) = n: Exit For
ElseIf drr(i, 1) = "邻" Then
m1 = m1 + 1
i = j - 1: crr(m1, 2) = n: Exit For
Else
m2 = m2 + 1
i = j - 1: crr(m2, 3) = n: Exit For
End If
ElseIf drr(j, 1) = drr(i, 1) And j = UBound(drr) Then
If drr(i, 1) = "重" Then
m = m + 1
i = j - 1: crr(m, 1) = n + 1: Exit For
ElseIf drr(i, 1) = "邻" Then
m1 = m1 + 1
i = j - 1: crr(m1, 2) = n + 1: Exit For
Else
m2 = m2 + 1
i = j - 1: crr(m2, 3) = n + 1: Exit For
End If
ElseIf drr(j, 1) <> drr(i, 1) And j = UBound(drr) Then
If drr(i, 1) = "重" Then
m = m + 1
crr(m, 1) = 1
ElseIf drr(i, 1) = "邻" Then
m1 = m1 + 1
crr(m1, 2) = 1
Else
m2 = m2 + 1
crr(m2, 3) = 1
End If
If drr(j, 1) = "重" Then
m = m + 1
i = j - 1: crr(m, 1) = 1: Exit For
ElseIf drr(j, 1) = "邻" Then
m1 = m1 + 1
i = j - 1: crr(m1, 2) = 1: Exit For
Else
m2 = m2 + 1
i = j - 1: crr(m2, 3) = 1: Exit For
End If
End If
Next
Next
x = Application.Max(m, m1, m2)
ar = sht.[k1:m1]
br = sht.[u2:u20]
n = 0
ReDim cr(1 To UBound(br), 1 To 3)
Set d = CreateObject("scripting.dictionary")
For i = 1 To UBound(br)
n = n + 1
For j = 1 To UBound(ar, 2)
s = br(i, 1) & ar(1, j)
d(s) = n
Next
Next
For j = 1 To UBound(ar, 2)
For i = 1 To x
If crr(i, j) <> Empty Then
s = crr(i, j) & ar(1, j)
cr(d(s), j) = cr(d(s), j) + 1
End If
Next
Next
With sht
.[k2:m360,p2:r360,v2:x360].ClearContents
.[k2].Resize(UBound(brr), 3) = brr
.[p2].Resize(x, 3) = crr
.[v2].Resize(UBound(cr), 3) = cr
End With
Set d = Nothing
Beep
End Sub
|
评分
-
2
查看全部评分
-
|