|
heaven_911 发表于 2012-9-4 09:26
第一次没发上来 继续补上
因为水平不够,所以程序写的很长,还请高手多多指点,另请楼主看是否符合要求- Private Sub CommandButton1_Click()
- Dim Arr, Brr, Crr, Drr, Err, i%, j%, n%, m%, Dkey, Expln$
- Dim Dic1 As New Dictionary, Dic2 As New Dictionary
- Arr = Range("A1").CurrentRegion.Value
-
- '利用字典剔除品名重复项
- For i = 2 To UBound(Arr)
- If Not Dic1.Exists(Arr(i, 2)) Then
- n = n + 1
- Dic1(Arr(i, 2)) = Array(Arr(i, 2), Arr(i, 3))
- Dic2(Arr(i, 2)) = n
- End If
- Next
-
- Brr = WorksheetFunction.Transpose(WorksheetFunction.Transpose(Dic1.Items))
- Dic1.RemoveAll
- ReDim Crr(1 To UBound(Brr), 1 To 2)
- For i = 2 To UBound(Arr)
- n = Dic2(Arr(i, 2))
- If Crr(n, 2) = "" Then
- Crr(n, 1) = Arr(i, 4) - Arr(i, 5)
- Crr(n, 2) = Arr(i, 6)
- Else
- Crr(n, 1) = Crr(n, 1) + Arr(i, 4) - Arr(i, 5)
- '设置m表示,m=1表示入库,m=-1表示出库
- If Arr(i, 4) > 0 Then
- m = 1
- Else
- m = -1
- End If
-
- '先将已存在的数据进行拆分,并加入字典
- Drr = Split(Crr(n, 2), "+")
- Crr(n, 2) = ""
- For j = 0 To UBound(Drr)
- Err = Split(Drr(j), "*")
- If UBound(Err) = 1 Then
- Dic1(Err(0)) = Val(Err(1))
- Else
- Dic1(Err(0)) = 1
- End If
- Next
-
- '将需要对比计算的数据进行拆分,并加入字典
- Drr = Split(Arr(i, 6), "+")
- For j = 0 To UBound(Drr)
- Err = Split(Drr(j), "*")
- If UBound(Err) = 1 Then
- Dic1(Err(0)) = Dic1(Err(0)) + Val(Err(1)) * m
- Else
- Dic1(Err(0)) = Dic1(Err(0)) + Val(Err(0)) * m
- End If
- Next
-
- For Each Dkey In Dic1.Keys
-
- '将字典的数据用"+"号和"*"号连接起来
- Expln = ""
- If Dic1(Dkey) > 1 Then
- Expln = Dkey & "*" & Dic1(Dkey)
- ElseIf Dic1(Dkey) = 1 Then
- Expln = Dkey
- End If
-
- If Expln <> "" Then
- If Crr(n, 2) = "" Then
- Crr(n, 2) = Crr(n, 2) & Expln
- Else
- Crr(n, 2) = Crr(n, 2) & "+" & Expln
- End If
- End If
- Next
- End If
- Dic1.RemoveAll
- Next
-
- With Sheets("基础表")
- .Cells.ClearContents
- .Activate
- .Range("A1:D1").Value = Array("品名", "单位", "结存", "备注")
- .Range("A2").Resize(UBound(Brr), UBound(Brr, 2)) = Brr
- .Range("C2").Resize(UBound(Crr), UBound(Crr, 2)) = Crr
- End With
- End Sub
复制代码 |
|