|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 最快法() '//By掘掘,郑广学的学生
Dim dic As Object, key As String, dickeys, Item, dicItems
Set dic = CreateObject("scripting.dictionary")
Set dic列 = CreateObject("scripting.dictionary")
arr = Sheet1.Range("a1").CurrentRegion
ReDim brr(1 To 10000, 1 To 50)
brr(1, 1) = "单位"
For j = 2 To UBound(arr, 2)
For i = 2 To UBound(arr, 1)
key = arr(i, 1) & ""
If Not dic.Exists(key) Then
行号 = dic.Count + 3 '
dic(key) = 行号 '//记录行号
brr(行号, 1) = arr(i, 1)
End If
行号 = dic(key) '//取出行号
key = arr(i, j)
If Not dic列.Exists(key) Then
列号 = dic列.Count + 2 '//从第3行开始输出
dic列(key) = 列号 '//记录行号
brr(1, 列号) = arr(1, j)
brr(2, 列号) = key
End If
列号 = dic列(key) '//取出列号
brr(行号, 列号) = brr(行号, 列号) + 1
Next
Next
For j = 2 To dic列.Count + 1 '//从列开始,下标法最大列号+1列单位为总列数
For i = 3 To 行号 '两行是标题,从第三行开始合计
brr(行号 + 1, j) = brr(行号 + 1, j) + brr(i, j)
Next
Next
brr(行号 + 1, 1) = "合计"
Sheet1.Range("h1").Resize(行号 + 1, UBound(brr, 2)) = brr: Sheet1.Range("h1").Resize(2, 1).Merge
横向合并拆分相容单元格 Sheet1.Range("h1").Resize(1, dic列.Count + 1), 1 '/默认合并
End Sub
Sub 横向合并拆分相容单元格(selection, Optional n As Boolean = True)
Dim rs, j
Application.ScreenUpdating = False
Application.DisplayAlerts = False
rs = selection.Columns.Count
If n Then
For j = rs To 2 Step -1
If selection.Cells(1, j) & "" = selection.Cells(1, j - 1) & "" Then
Union(selection.Cells(1, j), selection.Cells(1, j - 1)).Merge
End If
Next
Else
For j = 1 To rs
If selection.Cells(1, j).MergeCells = True Then
填充内容 = selection.Cells(1, j).MergeArea(1)
合并列数 = selection.Cells(1, j).MergeArea.Count
selection.Cells(1, j).MergeArea.UnMerge
selection.Cells(1, j).Resize(1, 合并列数) = 填充内容
j = j + 合并列数 - 1
End If
Next
End If
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub |
|