2016-10-27 53 views
0

什麼是合併相似行(只有訂單號字母不同於a4; a6; a8和生產數量)和求和(生產數量e4; e6; e8)單元格的最佳方式?這是怎麼練成表看起來 enter image description hereExcel合併相似的行和求和單元

澄清: 這裏是輸出我在尋找 enter image description here

行4; 6; 8,除了順序列(加月6日和8一個字母相同)和生產專欄(不同生產數量)。第4,6,8行合併併產生數量。行6,8被隱藏或刪除。

+0

你能澄清你的意思是「合併同類行「?也許你期望輸出看起來像樣本會有所幫助。你試過什麼了? – BruceWayne

+0

@BruceWayne嗨。我已經補充說明。 – ArnoldasM

回答

2

這裏是一個可以解決您的問題的例子:

Sub test() 

i = 1 
produced = 0 
While Cells(i, 1) <> "" Or Cells(i + 1, 1) <> "" 
    If Cells(i, 1) <> "" Then 

     produced = Cells(i, 5) 

     j = 1 
     'second loop to add up every line with the same order, then suppress the lines 
     While Cells(j, 1) <> "" Or Cells(j + 1, 1) <> "" 
      If Left(Cells(j, 1), 7) = Left(Cells(i, 1), 7) And i <> j Then 
       produced = produced + Cells(j, 5) 
       Cells(j, 5).EntireRow.Select 
       Selection.Delete Shift:=xlUp 
       j = j - 1 
      End If 

      j = j + 1 
     Wend 

    End If 

i = i + 1 
Wend 
+0

謝謝@Bitoubi求助。在我的excel表格生成列的完整版本是20,所以我已經將Cells(j,5)更改爲Cells(j,20)。我的訂單號碼是123456-7或123456-78(八或九個符號長度),所以我已將單元格(j,1),7更改爲單元格(j,1),但仍然無法工作。也許你知道它會是什麼錯誤?順便說一句,工作簿有200到500行。 – ArnoldasM

+0

謝謝,稍作修改,我設法使其工作。 :) – ArnoldasM

+1

不錯 - 我強烈建議雖然你[刪除使用'.Select'](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-宏)。該行將只是'Cells(j,5).EntireRow.Delete Shift:= xlUp' – BruceWayne

0

好吧,下面是修改@Bitoubi代碼,幫助我:

Sub RemoveSplitOrders() 
i = 1 
produced = 0 
While Cells(i, 1) <> "" Or Cells(i + 1, 1) <> "" 
    If Cells(i, 1) <> "" Then 

     produced = Cells(i, 20) 

     j = 1 
     'second loop to add up every line with the same order, then suppress the lines 
     While Cells(j, 1) <> "" Or Cells(j + 1, 1) <> "" 
      If Left(Cells(j, 1), 8) = Left(Cells(i, 1), 8) Or Left(Cells(j, 1), 9) = Left(Cells(i, 1), 9) Then 
       If Cells(j, 2) = Cells(i, 2) And i <> j Then 
        produced = produced + Cells(j, 20) 
        Cells(i, 20).Value = produced 
        Range(Cells(j, 20), Cells(j + 1, 20)).EntireRow.Delete Shift:=xlUp 
        j = j - 1 
       End If 
      End If 

      j = j + 1 
     Wend 

    End If 

i = i + 1 
Wend 
End Sub 
相關問題