2017-07-14 71 views
1

我正在尋找一種更有效的合併重複行的方法,具體取決於列名「Product」。有些行不會有重複。這是我正在使用的數據的一個示例。實際上,我正在處理數以千計的這些行和超過40列。如果根據列「產品」確定存在重複行,我的目標是合併到一行並保留非空值。VBA代碼合併重複行並保留非空值?

這裏是我的帖子在先生的鏈接。 Excel中,但沒有人能找出解決的辦法:https://www.mrexcel.com/forum/excel-questions/1014177-how-combine-rows-duplicate-info-into-one-based-column.html

這裏是之前的圖像和

image of before and after

上我怎麼可能讓這個過程更高效的任何想法,我想VBA代碼後」? 。需要我目前手動這樣做,這是非常痛苦的謝謝

回答

0
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」語句給你多少列有:)

+0

您好基蘭,這對我的作品,但你不介意解釋每一行做什麼。我是VBA的初學者。 – astro45

+0

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

0

可能是這樣的:

dim rRange as Range 
Set rRange = Application.InputBox('', '' , Type:=8) 

不記得確切..