|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Private Sub Worksheet_Change(ByVal Target As Range)
- If Application.Intersect(Target, [b3:c5]) Is Nothing Then Exit Sub
- Set ds = CreateObject("scripting.dictionary")
- Set dj = CreateObject("scripting.dictionary")
- crr = [a6:o13]
- arr = Sheets("销售数据源").[a1].CurrentRegion
- For j = 2 To UBound(arr)
- If arr(j, 4) = [b3] And InStr(arr(j, 5), [b4]) > 0 And InStr(arr(j, 6), [b5]) > 0 Then
- ds(arr(j, 1) & arr(j, 7)) = ds(arr(j, 1) & arr(j, 7)) + arr(j, 9)
- dj(arr(j, 1) & arr(j, 7)) = dj(arr(j, 1) & arr(j, 7)) + arr(j, 10)
- End If
- Next j
- arr = Sheets("库存数据源").[a1].CurrentRegion
- For j = 2 To UBound(arr)
- If arr(j, 4) = [b3] And InStr(arr(j, 5), [b4]) > 0 And InStr(arr(j, 6), [b5]) > 0 Then
- ds(arr(j, 1) & arr(j, 7)) = ds(arr(j, 1) & arr(j, 7)) + arr(j, 9)
- dj(arr(j, 1) & arr(j, 7)) = dj(arr(j, 1) & arr(j, 7)) + arr(j, 10)
- End If
- Next j
- For j = 3 To UBound(crr)
- sm = 0
- jm = 0
- For i = 2 To 7
-
- If ds.exists(crr(j, 1) & crr(2, i)) Then
- crr(j, i) = ds(crr(j, 1) & crr(2, i))
- crr(j, i + 7) = dj(crr(j, 1) & crr(2, i))
- Else
- crr(j, i) = 0
- crr(j, i + 7) = 0
- End If
- sm = crr(j, i) + sm
- jm = crr(j, i + 7) + jm
- Next i
-
- crr(j, 8) = sm
- crr(j, 15) = jm
-
- Next j
- For j = 2 To UBound(crr, 2)
- crr(5, j) = crr(4, j) + crr(3, j)
- crr(8, j) = crr(6, j) + crr(7, j)
- Next j
- [a6:o13] = crr
- End Sub
- Private Sub Worksheet_SelectionChange(ByVal Target As Range) '三级下拉菜单
- On Error Resume Next
- If Target.Address <> "$B$3:$C$3" And Target.Address <> "$B$4:$C$4" And Target.Address <> "$B$5:$C$5" Then Exit Sub
- Dim arr, d As Object, i&, x$
- Set d = CreateObject("scripting.dictionary")
- arr = Sheets("销售数据源").[a1].CurrentRegion
- For i = 2 To UBound(arr)
- x = arr(i, 4)
- If Not d.exists(x) Then
- Set d(x) = CreateObject("Scripting.Dictionary")
- d(x)(arr(i, 5) & "") = arr(i, 6)
- ElseIf InStr("," & d(x)(arr(i, 5) & "") & ",", "," & arr(i, 6) & ",") = 0 Then
- d(x)(arr(i, 5) & "") = d(x)(arr(i, 5) & "") & "," & arr(i, 6)
- End If
- Next
- ' Sheet11.Unprotect
- With Target.Validation
- .Delete
- Select Case Target.Address
- Case "$B$3:$C$3"
- .Add xlValidateList, , , Join(d.keys, ",")
- [b4:b5] = ""
- Case "$B$4:$C$4"
- .Add xlValidateList, , , Join(d([b3].Value).keys, ",")
- [b5] = ""
- Case "$B$5:$C$5"
- .Add xlValidateList, , , d([b3].Value)([b4].Value)
- End Select
- End With
- ' Sheet11.Protect UserInterfaceOnly:=True
- End Sub
复制代码 |
|