2015-12-09 70 views
0

我試圖創建一個按月分組數據的宏:例如,如果客戶端A有2個2月份條目,那麼它會將三個條目合併爲一個,然後將金額。按月分組數據的宏

我:

答:客戶名稱

B:發票號碼

C:開票月份

d:貨幣

E:收費金額

F:發票步驟

Column titles here

我所要做的,就是下面的成單排組,發票金額加在一起,並且只用一行替換三行。這些條目是針對單個客戶端的(因此分組取決於列A中的值)。

Example of entries here

EG。客戶端A有一個三個條目,客戶端B有一個,客戶端C有一個。然後對於二月份的客戶端A有一個,客戶端B有一個,客戶端C有兩個。

我在這裏看到的任何宏觀建議都沒有對我有幫助,我不斷收到錯誤,所以我不知道問題出在哪裏。這是一個我想:

Sub Group() 

Dim e As Range, a as Range 

Set e = Range("C6") 
Set c = e.Offset(, 2) 

Do 
If Evaluate("=month(" & e.Address & ")") <> Evaluate("=month(" & e(2).Address & ")") Then 
    e(2).Resize(2, 3).Insert 
    e(2).Offset(, 2) = "=sum(" & Range(a, c.Offset(, 2)).Address & ")" 
    e(2).Offset(, 2).Font.Bold = 1 
    Set e = e.End(4) 
    Set c = e.Offset(, 2) 
Else 
    Set e = e(2) 
End If 
Loop Until e.End(4).Row = Rows.Count 

e(2).Offset(, 2) = "=sum(" & Range(c, e.Offset(, 2)).Address & ")" 
e(2).Offset(, 2).Font.Bold = 1 

End Sub 

編輯:分類彙總和數據透視表是行不通的 - 原始數據被粘貼(千元的入門的線,爲客戶40多個每個月一定有很多發票),原始數據使用宏進行排序,然後將其粘貼到其他工作表中。從關鍵點粘貼會更困難。

+0

'任何宏建議,我已經在這裏看到的對我沒有幫助,我不斷收到錯誤,所以我不知道問題是什麼。這是我嘗試過的那個.....'爲你解答爲什麼你的例子不起作用將是非常有益的。你得到了什麼錯誤和什麼線路。 – psubsee2003

+2

您似乎在試圖重塑小計。 – pnuts

+1

或者您也可以使用數據透視表(如果您必須使用VBA,則需要編碼) –

回答

1

我按照你的形象寫的代碼: enter image description here

代碼後:

enter image description here

下面是代碼:

Sub TEST() 
Dim lastrow As Long 

lastrow = Range("A" & Rows.Count).End(xlUp).Row 



For i = 6 To lastrow 

lastrow = Range("A" & Rows.Count).End(xlUp).Row 

    For j = i + 1 To lastrow 

      If Range("A" & j) = Range("A" & i) And Range("C" & j) = Range("C" & i) Then 

       Range("B" & i) = Range("B" & i) & "," & " " & Range("B" & j) 
       Range("E" & i) = Range("E" & i).Value + Range("E" & j).Value 
       Rows(j).EntireRow.Delete 

      End If 

    Next j 

Next i 



End Sub 
+0

謝謝!這工作完美。 –

+0

歡迎您! – manu