|
直接举例说明吧,在此只是为了留个记录
表头1 | 表头2 | 表头3 | 表头4 | A | B | C | F | A | B | D | G | A | B | E | H |
处理成
表头1 | 表头2 | 表头3 | 表头4 | 表头5 | 表头6 | 表头7 | 表头8 | 表头9 | 表头10 | A | B | C | F | D | G | E | H | | |
- Sub MergeValuesBasedOnColumnA()
- Dim ws As Worksheet
- Set ws = ThisWorkbook.Sheets("Sheet1")
- Dim lastRow As Long
- lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
- Dim dict As Object
- Set dict = CreateObject("Scripting.Dictionary")
- Dim i As Long
- Dim key As Variant
- Dim valueList As Collection
- ' 遍历2列
- Temp = ""
- For i = 2 To lastRow
- For j = i To lastRow
- If (Cells(j, 2) <> Cells(j + 1, 2)) Then Exit For
- Next j
-
- For k = i To j
- Cells(j, 39 + 2 * (k - i)) = Cells(k, 15).Value
- Cells(j, 39 + 2 * (k - i) + 1) = Cells(k, 17).Value
-
-
- Next k
- i = j
- ' If (Cells(i, 2) = Cells(i + 1, 2)) Then
- '
- ' k = i
- ' Cells(k, 39 + i - k) = Cells(i, 15).Value
- ' Cells(k, 39) = Cells(i, 15).Value
- ' Temp = Cells(i, 15).Value & Cells(i, 17).Value
- ' Else
- '
- ' k = i
- ' Cells(i, 18) = Temp & Chr(10) & Cells(i, 15).Value & Cells(i, 17).Value
- '
- ' Temp = ""
- '
- ' End If
- Next i
- End Sub
复制代码
用到了双for循环,以及循环变量的阶段跳跃处理,注释掉的是合并到同单元格处理方式。思路仅供参考
|
|