改一下。。。
- Sub ykcbf() '//2024.11.22
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Set d = CreateObject("Scripting.Dictionary")
- ReDim brr(1 To 10000, 1 To 100)
- On Error Resume Next
- M = 2: n = 3
- With Sheets("基础数据")
- r = .Cells(Rows.Count, 1).End(3).Row
- zrr = .[a1].Resize(r, 7)
- Set Rng = .[a2].Resize(r - 1, 7)
- With Rng
- .Parent.Sort.SortFields.Clear
- .Sort key1:=.Item(1), order1:=1, key2:=.Item(5), order2:=1, Header:=2
- End With
- arr = Rng.Value
- .[a1].Resize(r, 7) = zrr
- End With
- For j = 2 To 4
- brr(1, j - 1) = arr(1, j)
- Next
- For i = 2 To UBound(arr)
- s = arr(i, 2) & "|" & arr(i, 3) & "|" & arr(i, 4)
- If Not d.exists(s) Then
- M = M + 1
- d(s) = M
- brr(M, 1) = arr(i, 2)
- brr(M, 2) = arr(i, 3)
- brr(M, 3) = arr(i, 4)
- End If
- r = d(arr(i, 2) & "|" & arr(i, 3) & "|" & arr(i, 4))
- s = arr(i, 1) & "|" & Format(arr(i, 5), "yyyy年m月")
- If Not d.exists(s) Then
- n = n + 1
- d(s) = n
- brr(1, n) = arr(i, 1)
- brr(2, n) = Format(arr(i, 5), "yyyy年m月")
- End If
- c = d(arr(i, 1) & "|" & Format(arr(i, 5), "yyyy年m月"))
- brr(r, c) = brr(r, c) + arr(i, 6)
- Next
- With Sheets("汇总")
- .UsedRange.Clear
- .[a1].Resize(2, n).Interior.Color = 49407
- With .[a1].Resize(M, n)
- .Value = brr
- .Borders.LineStyle = 1
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- With .Font
- .Name = "微软雅黑"
- .Size = 11
- End With
- End With
- For j = 1 To 3
- .Cells(1, j).Resize(2).Merge
- Next
- For j = n To 4 Step -1
- Set Rng = ActiveSheet.Cells(1, j)
- Set Rng1 = Rng.Offset(, -1)
- If Rng = Rng1 Then
- Rng1.Resize(, 2).Merge '//标题列向合并
- End If
- Next
- .[c2].Resize(M - 1).ShrinkToFit = True
- End With
- Application.ScreenUpdating = True
- MsgBox "OK!"
- End Sub
复制代码
|