|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 xiao0527 于 2019-1-1 11:21 编辑
进入论坛已经有段时间了,还是局限用一些公式,有点时间也想看看vba.对数组 字典了解比较少,看了版主的字典学习,有许多问题了解比如字典数组和数组
如代码
Sub bbhz()
Dim i&, Myr&, x(1 To 3), Arr, n%, aa, j&, Arr1(), r%, Arr2(), r2%, r3%, Arr3()
Dim d(1 To 3) As New dictionary, k(1 To 3), t(1 To 3), js, ks, ii%, jj&, ks1, js1
Application.ScreenUpdating = False
Myr = Sheet1.[a65536].End(xlUp).Row
Arr = Sheet1.Range("a3:g" & Myr)
For i = 1 To UBound(Arr)
x(1) = Arr(i, 2)
d(1)(x(1)) = d(1)(x(1)) + Arr(i, 3)
x(2) = Arr(i, 2) & "|" & Arr(i, 4)
d(2)(x(2)) = d(2)(x(2)) + Arr(i, 5)
x(3) = Arr(i, 2) & "|" & Arr(i, 4) & "|" & Arr(i, 6)
d(3)(x(3)) = d(3)(x(3)) + Arr(i, 7)
Next
For i = 1 To 3
k(i) = d(i).Keys
t(i) = d(i).Items
Next
Sheet4.Activate
[a3:k1000].ClearContents
[a3:k1000].UnMerge
[a3:k1000].Borders.LineStyle = xlNone
[i3].Resize(d(3).Count, 1) = Application.Transpose(t(3))
n = 2
For i = 0 To UBound(k(3))
aa = Split(k(3)(i), "|")
n = n + 1
Cells(n, 2) = aa(0)
Cells(n, 4) = aa(1)
Cells(n, 8) = aa(2)
Next
For i = 3 To n
For j = 0 To UBound(k(1))
If Cells(i, 2) = k(1)(j) Then
Cells(i, 3) = t(1)(j)
Cells(i, 10) = Cells(i, 9) / Cells(i, 3)
Cells(i, 11) = Cells(i, 10): Exit For
End If
Next
For j = 0 To UBound(k(2))
If Cells(i, 2) & "|" & Cells(i, 4) = k(2)(j) Then
Cells(i, 5) = t(2)(j)
Cells(i, 6) = Cells(i, 5) / Cells(i, 3)
Cells(i, 7) = Cells(i, 6): Exit For
End If
Next
Next
Range("a3:k" & n).Sort Key1:=Range("b3"), Order1:=xlAscending, Key2:=Range("d3") _
, Order2:=xlAscending, Key3:=Range("h3"), Order3:=xlAscending, Header:= _
xlGuess
For i = 3 To n
If Cells(i, 2) <> Cells(i - 1, 2) Then
r = r + 1
ReDim Preserve Arr1(1 To r)
Arr1(r) = i
End If
Next
Application.DisplayAlerts = False
For j = 1 To r
r3 = 0: r2 = 0
If j <> r Then
js = Arr1(j + 1) - 1
Else
js = n
End If
ks = Arr1(j)
If js - ks + 1 > 1 Then
Cells(ks, 1).Resize(js - ks + 1, 1).Merge
Cells(ks, 2).Resize(js - ks + 1, 1).Merge
Cells(ks, 3).Resize(js - ks + 1, 1).Merge
End If
Cells(ks, 1) = j
For ii = ks To js
If ii = ks Then
r2 = r2 + 1
ReDim Preserve Arr2(1 To r2)
Arr2(r2) = ii
ElseIf Cells(ii, 4) <> Cells(ii - 1, 4) Then
r2 = r2 + 1
ReDim Preserve Arr2(1 To r2)
Arr2(r2) = ii
End If
Next
For ii = 1 To r2
If ii <> r2 Then
js1 = Arr2(ii + 1) - 1
Else
js1 = js
End If
ks1 = Arr2(ii)
If js1 - ks1 + 1 > 1 Then
Cells(ks1, 4).Resize(js1 - ks1 + 1, 1).Merge
For jj = ks1 To js1
If jj <> ks1 Then
Cells(ks, 7) = Cells(ks, 7) + Cells(jj, 7)
End If
Next
Cells(ks1, 5).Resize(js1 - ks1 + 1, 1).Merge
Cells(ks1, 6).Resize(js1 - ks1 + 1, 1).Merge
Else
If ii <> 1 Then
Cells(ks, 7) = Cells(ks, 7) + Cells(ks1, 7)
End If
End If
Next
Cells(ks, 7).Resize(js - ks + 1, 1).Merge
For ii = ks To js
If ii = ks Then
r3 = r3 + 1
ReDim Preserve Arr3(1 To r3)
Arr3(r3) = ii
ElseIf Cells(ii, 8) <> Cells(ii - 1, 8) Then
r3 = r3 + 1
ReDim Preserve Arr3(1 To r3)
Arr3(r3) = ii
End If
Next
For ii = 1 To r3
If ii <> r3 Then
js1 = Arr3(ii + 1) - 1
Else
js1 = js
End If
ks1 = Arr3(ii)
If js1 - ks1 + 1 > 1 Then
Cells(ks1, 8).Resize(js1 - ks1 + 1, 1).Merge
For jj = ks1 To js1
If jj <> ks1 Then
Cells(ks1, 9) = Cells(ks1, 9) + Cells(jj, 9)
Cells(ks1, 10) = Cells(ks1, 10) + Cells(jj, 10)
End If
Cells(ks, 11) = Cells(ks, 11) + Cells(jj, 11)
Next
Cells(ks1, 9).Resize(js1 - ks1 + 1, 1).Merge
Cells(ks1, 10).Resize(js1 - ks1 + 1, 1).Merge
Else
If ii <> 1 Then
Cells(ks, 11) = Cells(ks, 11) + Cells(ks1, 11)
End If
End If
Next
Cells(ks, 11).Resize(js - ks + 1, 1).Merge
Next
Range("a3:k" & n).Borders.LineStyle = 1
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
刚看第一段,就搞不清楚如:x(1) = Arr(i, 2) x(1)的值是唯一的,Arr(i, 2)的变化 ,x(1)的值会不停被替换。理解是为变量x的一个元素赋值.前面x(1 To 3)应该是声明数组一维数组x.元素是1,2,3.
那么 声明数组一维数组k(1 To 3), t(1 To 3),
For i = 1 To 3
k(i) = d(i).Keys
t(i) = d(i).Items
Next 这个代码当 i =1时就是 k(1) = d(i).Keys为什么可以赋值数组呢,应该是为D的3个元素赋值可是d(i).Keys数组集合吗。
|
|