|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
练手,表格转换。- Sub ykcbf() '//2024.7.19 表格转换,并加上合计行
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Set d = CreateObject("Scripting.Dictionary")
- With Sheets("数据")
- r = .Cells(Rows.Count, 1).End(3).Row
- arr = .[a1].Resize(r, 5)
- End With
- ReDim brr(1 To r, 1 To 100)
- m = 2: n = 1
- brr(1, 1) = arr(1, 1)
- For i = 2 To UBound(arr)
- s = arr(i, 1)
- If Not d.exists(s) Then
- m = m + 1
- d(s) = m
- brr(m, 1) = s
- End If
- r = d(arr(i, 1))
- s = arr(i, 2)
- If Not d.exists(s) Then
- n = n + 3
- d(s) = n
- brr(1, n - 2) = s
- For j = 3 To UBound(arr, 2)
- brr(2, n - 5 + j) = arr(1, j)
- Next
- End If
- c = d(arr(i, 2))
- For j = 3 To UBound(arr, 2)
- brr(r, c - 5 + j) = arr(i, j)
- Next
- Next
- '//*******************************
- '//增加合计行数据
- m = m + 1
- brr(m, 1) = "合计"
- For j = 2 To n
- Sum = 0
- For i = 3 To m - 1
- Sum = Sum + brr(i, j) '//求每列合计
- Next
- brr(m, j) = Sum
- Next
- '//********************************
- With Sheets("结果")
- .UsedRange.Clear
- .Cells.Interior.ColorIndex = 0
- .[a1].Resize(2, n).Interior.Color = 49407
- .[a3].Resize(m - 2, 1).Interior.Color = 5296274
- .[a1].Resize(2).Merge
- With .[a1].Resize(m, n)
- .Value = brr
- .Borders.LineStyle = 1
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- With .Font
- .Name = "微软雅黑"
- .Size = 11
- End With
- For j = 2 To n Step 3
- .Cells(1, j).Resize(, 3).Merge
- Next
- End With
- End With
- Application.ScreenUpdating = True
- MsgBox "OK!"
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|