Sub compareLines()
'Set selected cell to starting position Row 2 Column A
ActiveSheet.Cells(2, 1).Select
'Stopping the application updating the screen while the macro is running which can significantly increase the speed of vba
Application.ScreenUpdating = False
'Loop to keep macro running into it reaches the last 'Product'
While ActiveCell.Value <> ""
'Check whether the product name in the next row is the same as the product in the current row
If ActiveCell.Value = ActiveCell.Offset(1, 0).Value Then
'Keep going until you reach the 40th column(change this to what u need)
For i = 2 To 40
'Checks whether the next column is blank
If ActiveCell.Offset(0, i).Value = "" Then
'If the column is in fact blank then copy the value of the row below
ActiveCell.Offset(0, i).Value = ActiveCell.Offset(1, i).Value
End If
'move to next column
Next
'Once the last column has been reached, delete the duplicate row
ActiveCell.Offset(1, 0).EntireRow.Delete
'If product below isn't the same as the current product
Else
'Then move to the next row
ActiveCell.Offset(1, 0).Select
End If
Wend
'turning this back on so you can see the changes
Application.ScreenUpdating = True
End Sub
更改“For”语句给你多少列有:)
您好基兰,这对我的作品,但你不介意解释每一行做什么。我是VBA的初学者。 – astro45
当然,我从原始答案中评论了我的代码。如果它适用于您,您能否将其标记为正确答案?干杯 –