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
當然,我從原始答案中評論了我的代碼。如果它適用於您,您能否將其標記爲正確答案?乾杯 –