![VBA Excel 宏可以将数据从一行附加到另一行](https://linux22.com/image/1378543/VBA%20Excel%20%E5%AE%8F%E5%8F%AF%E4%BB%A5%E5%B0%86%E6%95%B0%E6%8D%AE%E4%BB%8E%E4%B8%80%E8%A1%8C%E9%99%84%E5%8A%A0%E5%88%B0%E5%8F%A6%E4%B8%80%E8%A1%8C.png)
我有一个执行以下操作的宏:
- 检查连续行是否具有相同的数据(F 列和 G 列)
我还需要这个宏以某种方式执行以下操作:
- 如果 F 列和 G 列中的数据与下一行相同
- 将两行 A、B、C、D、I 和 J 列的数据合并到第一行的末尾。就像您按了 ALT + ENTER 并输入了数据一样
有任何想法吗?
Sub test()
'define variables
Dim RowNum as long, LastRow As long
'turn off screen updating
Application.ScreenUpdating = False
'start below titles and make full selection of data
RowNum = 2
LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row
Range("A2", Cells(LastRow, 10)).Select
'For loop for all rows in selection with cells
For Each Row In Selection
With Cells
'if first name matches
If Cells(RowNum, 5) = Cells(RowNum + 1, 5) Then
'and if last name matches
If Cells(RowNum, 6) = Cells(RowNum + 1, 6) Then
*******This is the part I cannot figure out!*******
Rows(RowNum + 1).EntireRow.Delete
End If
End If
End With
'increase rownum for next test
RowNum = RowNum + 1
Next Row
'turn on screen updating
Application.ScreenUpdating = True
End Sub
答案1
用于附加单元格的内容Alt+Enter样式使用以下代码:
Cells(RowNum, 1).Value = Cells(RowNum, 1).Value & Chr(10) & Cells(RowNum + 1, 1).Value
这将适用于A
列中的单元格。