2017-10-13 83 views
0

如果客戶和出生日期列在行之間匹配,我想總結收入。在VBA中合併行

account number customer Date of Birth Country $ Revenue StartD  
    47971   MARTY-ANE 27/12/1957  IF_FR_OU 100 10022010 
    48045   QUESNEL  06/05/1956  IF_FR_OU 200 11022010 
    47999   MARTY-ANE 27/12/1957  IF_IT_OU 100 12022010 

我想是這樣的:

account number customer Date of Birth Country $ Revenue StartD  
47971 & 47999 MARTY-ANE 27/12/1957 IF_FR_OU 200 10022 & 12022 
48045   QUESNEL 06/05/1956  IF_FR_OU 200 11022 

代碼:

Sub ProcessCustomers() 
Dim key As Variant, values As Variant, results As Variant 
Dim list As Object 
Dim x As Long, IndexOf As Long 
Set list = CreateObject("System.Collections.ArrayList") 
'account number|customer|Date of Birth|Country|$ Revenue 

    With Worksheets("Sheet1") 
    For x = 2 To .Range("A" & .Rows.Count).End(xlUp).Row 
     key = .Cells(x, 2).Value & "|" & .Cells(x, 3).Value & "|" & 
    .Cells(x, 4).Value 
     If list.Count = 0 Then ReDim results(0 To 4, 0 To 0) 

     If list.Contains(key) Then 
      IndexOf = list.LastIndexOf(key) 
      results(0, IndexOf) = results(0, IndexOf) & " & " & 
    .Cells(x, 1).Value 
      results(4, IndexOf) = results(4, IndexOf) + .Cells(x, 
    5).Value 
     Else 
      list.Add key 
      ReDim Preserve results(0 To 4, 0 To list.Count - 1) 
      IndexOf = list.Count - 1 
      results(0, IndexOf) = .Cells(x, 1).Value 
      results(1, IndexOf) = .Cells(x, 2).Value 
      results(2, IndexOf) = .Cells(x, 3).Value 
      results(3, IndexOf) = .Cells(x, 4).Value 
      results(4, IndexOf) = .Cells(x, 5).Value 
     End If 
    Next 
End With 
'Transpose results and change the Array Base from 0 to 1 
results = Application.Transpose(results) 
With Worksheets.Add 
    .Range("A1").Resize(1, 5).Value = Split("account 
number|customer|Date of Birth|Country|$ Revenue", "|") 
    .Range("A1").Resize(UBound(results), UBound(results, 2)).Value = 
results 
End With 
End Sub 
+1

和問題是.......... ?? –

+0

我原來的代碼只合併到收入,不包括開始D,我試過增加範圍,但沒有工作,我需要做什麼來得到這項工作? –

回答

0

無需兌換碼。按客戶,日期分類排序。然後使用數據,小計... 在每次更改日期,收入總和

+0

完成工作,但不會連接OP所要求的[Account Number]和[StartD]列值。雖然,我也不會打擾。取消downvoted。 –

+0

我需要使用vba自動運行,但將來的數據將會改變 –

+0

有人可以提供建議嗎? –