|
- Sub 分摊()
- Dim d, ar, br(), brr, i, j, x, m, n, k, t, s, a
- Set d = CreateObject("Scripting.Dictionary")
- Sheet3.[A3:I10000] = Empty
- ar = Sheet2.Range("A1").CurrentRegion
- For i = 2 To UBound(ar)
- s = Trim(ar(i, 1)) & Trim(ar(i, 3))
- If d(s) = "" Then
- m = m + 1
- ReDim Preserve br(1 To 10, 1 To m)
- d(s) = m
- br(1, m) = Trim(ar(i, 1))
- br(2, m) = Trim(ar(i, 3))
- br(3, m) = ar(i, 5)
- br(4, m) = ar(i, 6)
- br(5, m) = ar(i, 2)
- If ar(i, 5) <> "WI" Then br(10, m) = ar(i, 2)
- Else
- br(5, d(s)) = br(5, d(s)) + ar(i, 2)
- If ar(i, 5) <> "WI" Then br(10, d(s)) = br(10, d(s)) + ar(i, 2)
- End If
- Next
- d.RemoveAll
- ar = Sheet3.Range("L2").CurrentRegion
- For i = 2 To UBound(ar)
- For j = 2 To UBound(ar, 2)
- If Not d.exists(Trim(ar(i, 1))) Then
- n = 1
- ReDim brr(1 To 2, 1 To n)
- Else
- brr = d(Trim(ar(i, 1)))
- n = UBound(brr, 2) + 1
- ReDim Preserve brr(1 To 2, 1 To n)
- End If
- brr(1, n) = Trim(ar(1, j))
- brr(2, n) = ar(i, j)
- d(Trim(ar(i, 1))) = brr
- Next
- Next
- k = d.keys
- t = d.items
- For j = 1 To m
- If br(3, j) <> "WI" Then
- For x = 0 To d.Count - 1
- For i = 1 To UBound(t(x), 2)
- If CStr(br(1, j)) = CStr(t(x)(1, i)) Then
- a = 0
- For y = 1 To m
- If CStr(br(1, y)) = CStr(t(x)(1, i)) Then a = a + br(10, y)
- Next
- br(x + 6, j) = t(x)(2, i) / a * br(5, j)
- End If
- Next
- Next
- End If
- Next
- Sheet3.[A3].Resize(m, 9) = Application.Transpose(br)
- Set d = Nothing
- End Sub
复制代码 |
|