|
- Sub test2() '数组+字典 再练习一下,也自动生成
- Dim ar(), br, cr, Dict As Object, strKey As String
- Dim i As Long, j As Long, k As Long
- Dim posRow As Long, posCol As Long, colSize As Long, rowSize As Long
-
- 'Application.ScreenUpdating = False
-
- colSize = 4
- rowSize = 1
- cr = Split("应交,已交, 抵消", ",")
- br = Sheet2.Range("A1").CurrentRegion
- ReDim ar(1 To UBound(br), 1 To 40)
-
- Set Dict = CreateObject("Scripting.Dictionary")
- For j = 1 To colSize
- ar(rowSize, j) = Split("代码 单位名称 性质 总计")(j - 1)
- If j < 3 Then Dict.Add cr(j - 1), j - 1
- Next
-
- For i = 2 To UBound(br)
- strKey = Format(br(i, 1), "YYYY-MM")
- If Dict.Exists(strKey) Then
- posCol = Dict(strKey)
- Else
- colSize = colSize + 1
- posCol = colSize
- ar(1, colSize) = strKey
- Dict.Add strKey, colSize
- End If
-
- strKey = br(i, 2) & "|" & br(i, 3)
- If Dict.Exists(strKey) Then
- posRow = Dict(strKey)
- Else
- rowSize = rowSize + 3
- posRow = rowSize - 2
- Dict.Add strKey, posRow
- For k = 0 To 1
- For j = 1 To 2
- ar(posRow + k, j) = br(i, 1 + j)
- Next
- ar(posRow + k, j) = cr(k)
- Next
- ar(posRow + k, j - 1) = br(i, j) & cr(2)
- End If
- posRow = posRow + Dict(br(i, 4))
- ar(posRow, 4) = ar(posRow, 4) + Val(br(i, 5))
- ar(posRow, posCol) = ar(posRow, posCol) + Val(br(i, 5))
- Next
-
- For i = 4 To rowSize Step 3
- For j = 4 To colSize
- ar(i, j) = Val(ar(i - 2, j)) - Val(ar(i - 1, j))
- Next j, i
- BubbleSort ar, 1, rowSize, 5, colSize, 1
-
- With Sheet1.Range("A5")
- .CurrentRegion.Clear
- With .Resize(rowSize, colSize)
- .Borders.Weight = xlHairline
- .HorizontalAlignment = xlCenter
- .Columns(1).NumberFormatLocal = "@"
- .Rows(1).Font.Bold = True
- .Value = ar
- End With
- End With
-
- Set Dict = Nothing
- Application.ScreenUpdating = True
- Beep
- End Sub
- Function BubbleSort(ar, u As Long, d As Long, l As Long, r As Long, pos As Long)
- Dim j As Long, x As Long, y As Long, Flag As Boolean, Swap
- For j = l To r - 1
- Flag = True
- For x = l To r + l - 1 - j
- If CDate(ar(pos, x)) > CDate(ar(pos, x + 1)) Then
- Flag = False
- For y = u To d
- Swap = ar(y, x)
- ar(y, x) = ar(y, x + 1)
- ar(y, x + 1) = Swap
- Next
- End If
- Next
- If Flag = True Then Exit For
- Next
- End Function
复制代码 |
评分
-
1
查看全部评分
-
|