|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Option Explicit
- '专业VBA开发,生产优质代码!
- Sub 汇总()
- Dim tmpBook As Workbook
- Set tmpBook = Workbooks.Add
- Dim i&
- Dim endrow&, r&
- r = 1
- With ThisWorkbook
- For i = 2 To .Sheets.Count
- With .Sheets(i)
- endrow = .Cells(Rows.Count, 4).End(xlUp).Row
- .UsedRange.Copy tmpBook.ActiveSheet.Cells(r, 1)
- r = r + endrow
- End With
- Next
- End With
-
- Dim tmpRange As Range, 月列&
- With ThisWorkbook
- On Error Resume Next
- Set tmpRange = ThisWorkbook.Sheets(2).Rows(1).Find(.Sheets("总").[D3].Value)
- Set tmpRange = ThisWorkbook.Sheets(3).Rows(1).Find(.Sheets("总").[D3].Value)
- On Error GoTo 0
- If tmpRange Is Nothing Then
- MsgBox "未找到指定月所在列"
- Exit Sub
- End If
- 月列 = tmpRange.Column
- End With
-
-
- Dim arr
- With tmpBook
- Dim cell As Range
- Dim mergedCellRange As Range
-
- For Each cell In .ActiveSheet.UsedRange
-
- If cell.MergeCells Then
- Set mergedCellRange = cell.MergeArea
- With mergedCellRange
- '获取合并单元格的内容
- Dim cellText As String
- cellText = cell.Value
-
- '取消合并单元格
- mergedCellRange.UnMerge
- cell.Value = cellText
-
- '将单元格内容填充到所有拆分出的单元格中
- For i = 1 To .Cells.Count
- .Cells(i).Value = cellText
- Next
- End With
- End If
- Next
- arr = .ActiveSheet.UsedRange
- .Close False
- End With
-
-
- Dim d As Object
- Set d = CreateObject("Scripting.Dictionary")
-
- For i = 1 To UBound(arr)
- d(arr(i, 2) & "_" & arr(i, 4)) = arr(i, 月列)
- Next
- Dim j&, endCol&
-
- With ThisWorkbook.Sheets("总")
- Call mkTitleDic(.UsedRange.Rows(2), d)
- endCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
- endrow = .Cells(Rows.Count, 3).End(xlUp).Row
-
- For i = 3 To endrow
- For j = 5 To endCol
- .Cells(i, j) = d(.Cells(i, "C").Value & "_" & .Cells(2, j).Value)
- Next
- Next
-
- End With
-
- MsgBox "完成!"
-
- End Sub
- Sub mkTitleDic(sRange As Range, d)
- '制作列号字典
- 'call mkTitleDic(范围,字典)
- Dim eachCell
- For Each eachCell In sRange.Cells
- d(eachCell.Value) = eachCell.Column
- Next
- End Sub
复制代码
支持列名增加,改名,换位置。应该是比较完善的一个版本。 |
评分
-
1
查看全部评分
-
|