- Option Explicit
- Dim ar
- Private Sub Worksheet_Change(ByVal Target As Range)
- If Target.Address <> "$E$2" Then Exit Sub
- Dim sName$, br(), sa, i, x, j
- ReDim br(1 To UBound(ar), 1 To 8)
- Range("b3").CurrentRegion.Offset(2).ClearContents
- sName = Target.Value
- sa = Array(3, 4, 7, 10, 16, 17)
- x = 1
- br(1, 4) = "期初余额"
- br(1, 8) = 0
- For i = 3 To UBound(ar)
- If ar(i, 13) & "-" & ar(i, 14) = sName Then
- x = x + 1
- For j = 0 To 5
- br(x, j + 1) = ar(i, sa(j))
- Next
- br(x, 8) = br(x - 1, 8) + br(x, 5) - br(x, 6)
- End If
- Next
- Range("b4").Resize(x, 8) = br
- End Sub
- Private Sub Worksheet_Activate()
- Dim d As Object, i&
- Set d = CreateObject("Scripting.Dictionary")
- ar = Sheet2.UsedRange
- For i = 3 To UBound(ar)
- If ar(i, 13) <> "" Then d(ar(i, 13) & "-" & ar(i, 14)) = ""
- Next
- With Range("E2").Validation
- .Delete
- .Add 3, 1, 1, Join(d.keys, ",")
- End With
- End Sub
复制代码 |