我希望根据代码列中的值将多行合并为一行。我注意到这里有其他与我的问题类似的问题,但我似乎无法扩大范围。
Code Name Value A Value B Value C Value D Value E
101 Example #
101 Example
101 Example #
101 Example #
101 Example #
102 Example2 #
102 Example2 #
102 Example2 #
102 Example2 #
102 Example2 #
最终结果如下:
Code Name Value A Value B Value C Value D Value E
101 Example # # # #
102 Example2 # # # # #
编辑
这是我目前得到的,我的计划是将项目移至上面的行,然后删除整行,因为一行可能有许多项目。
Dim RowNum, LastRow, Col As Long
RowNum = 2
Col = 3
LastRow = Cells.SpecialCells(xlCellTypeLastCell).row
Range("A2", Cells(LastRow, 7)).Select
For Each row In Selection
With Cells
If Cells(RowNum, 1) = Cells(RowNum + 1, 1) Then
For Each Cell In row
If Cell > 0 Then
Cells(RowNum + 1, Col).Copy Destination:=Cells(RowNum, Col)
Else
Col = Col + 1
End If
Rows(RowNum + 1).EntireRow.Delete
End If
End With
RowNum = RowNum + 1
Next row
答案1
我对你基于该代码得出的答案感到有点惭愧。备份您的数据并在副本上进行测试!
这应该有效:
Sub combine()
Dim c As Range
Dim i As Integer
For Each c In Range("A2", Cells(Cells.SpecialCells(xlCellTypeLastCell).Row, 1))
If c = c.Offset(1) And c <> "" Then
For i = 1 To 6
If c.Offset(1, i) <> "" Then
c.Offset(, i) = c.Offset(1, i)
End If
Next
c.Offset(1).EntireRow.Delete
End If
Next
End Sub