|
楼主 |
发表于 2014-8-26 15:13
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
自问自答:
如果在数据源表上“金额”列前增加插入三列,则需修改代码如下:
Sub 物料用途整理()
Dim nR1%, nL1%, nR2&, nR%, nL%, nJe, arr(), brr()
Dim ds As Object
Set ds = CreateObject("Scripting.Dictionary") '创建字典
With Sheets("目标表")
nR1 = .Range("b4").End(xlDown).Row
nL1 = .Range("b4").End(xlToRight).Column
.Range(.Range("d6"), .Cells(nR1, nL1)).ClearContents
arr = .Range(.Range("b4"), .Cells(nR1, nL1)).Value
End With
With Sheets("数据源表")
nR2 = .Range("a3").End(xlDown).Row
brr = .Range("b4:h" & nR2).Value
End With
For i = 4 To nL1 - 1
ds(arr(2, i)) = i
Next
For i = 4 To nR1 - 3
ds(arr(i, 1) & arr(i, 2)) = i
Next
For i = 1 To nR2 - 3
nJe = brr(i, 7)
nL = ds(Left(brr(i, 3), 4))
nR = ds(brr(i, 1) & brr(i, 2))
If nL * nR * nJe <> 0 Then
arr(3, 3) = arr(3, 3) + nJe
arr(3, nL) = arr(3, nL) + nJe
arr(nR, nL) = arr(nR, nL) + nJe
arr(nR, 3) = arr(nR, 3) + nJe
nR = ds(brr(i, 1) & "合计")
If nR > 0 Then
arr(nR, nL) = arr(nR, nL) + nJe
arr(nR, 3) = arr(nR, 3) + nJe
End If
End If
Next
With Sheets("目标表")
.Range(.Range("b4"), .Cells(nR1, nL1)).Value = arr
End With
End Sub |
|