我有一个执行以下操作的宏:
- 检查连续行是否具有相同的数据(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
列中的单元格。