返回列表 上一主題 發帖

[發問] 2個條件的加總

[發問] 2個條件的加總

請問各位高手,

我要將原始資料裡依類別與日期這兩個條件將金額加總到"總表",例如:1/1~1/31, A類金額加總後放到B4儲存格,2/1~2/28, A類金額加總後放到C4儲存格,3/1~3/31, A類金額加總後放到D4儲存格..以此類推
原本使用工作表函數SUMPRODUCT, 但資料量多時,工作表run重新計算會花很多時間, 想改用VBA方式.
但我只會用下列寫法,發現這樣寫法要寫很多的IF判斷,想請問各位高手 提供較好的寫法.感謝!

Private Sub CommandButton2_Click()
Dim rowcnt As Integer
Dim i As Integer
Dim sum_A_1, sum_A_2, sum_A_3,sum_A_4,sum_A_5,sum_A_6,sum_A_7,sum_A_8,sum_A_9,sum_A_10,sum_A_11,sum_A_12 As Variant
sum_A_1= 0 '統計A類1月份金額
sum_A_2= 0 '統計A類2月份金額
sum_A_3= 0 '統計A類3月份金額
sum_A_4= 0 '統計A類4月份金額
sum_A_5= 0 '統計A類5月份金額

Sheets("原始資料").Activate
With Sheets("原始資料")
rowcnt= .Cells(1, 1).CurrentRegion.Rows.Count

For i = 1 To rowcnt
    '加總A類一月份金額
    If .Cells(i,2) >= "2013/1/1"And .Cells(i, 2) <= "2013/1/31" And .Cells(i,1) = Sheets("總表").Range("A4") Then
        sum_A_1= sum_A_1 + (Cells(i, 3).Value)
        Sheets("總表").Range("B4") = sum_A_1
    End If
   '加總A類二月份金額
     If .Cells(i,2) >= "2013/2/1"And .Cells(i, 2) <= "2013/2/28" And .Cells(i,1) = Sheets("總表").Range("A4") Then
        sum_A_2= sum_A_2+ (Cells(i, 3).Value)
        Sheets("總表").Range("C4") = sum_A_2   
     End If
Next

End With
End Sub

圖二.jpg (59.32 KB)

圖二.jpg

learner

本帖最後由 kimbal 於 2013-5-31 01:40 編輯

回復 1# yliu
  1. Private Sub CommandButton2_Click()
  2. Application.ScreenUpdating = False
  3. Application.Calculation = xlCalculationManual

  4. Dim rowcnt As Long
  5. Dim i As Long

  6. Dim lngVlookupRow As Long
  7. Dim lngCurrenMonth As Long
  8. Dim lngCurrValue As Long

  9. '清理舊數據
  10. Sheets("總表").Activate
  11. Sheets("總表").Range("A3").CurrentRegion.Offset(1, 1).Clear


  12. Sheets("原始資料").Activate
  13. With Sheets("原始資料")
  14.     rowcnt = .Cells(1, 1).CurrentRegion.Rows.Count
  15.     For i = 1 To rowcnt
  16.         lngCurrenMonth = 0
  17.         If IsDate(.Cells(i, 2)) Then
  18.             lngCurrenMonth = Month(.Cells(i, 2)) '當行月份
  19.         End If
  20.         If lngCurrenMonth > 0 And Not (IsError(Application.Match(.Cells(i, 1), Sheets("總表").Range("A:A"), 0))) Then
  21.             lngVlookupRow = Application.Match(.Cells(i, 1), Sheets("總表").Range("A:A"), 0) '當行類型在總表行數
  22.             lngCurrValue = .Cells(i, 3)
  23.             With Sheets("總表").Cells(lngVlookupRow, lngCurrenMonth + 1) '放數據到總表上
  24.                 .Value = .Value + lngCurrValue
  25.             End With
  26.         End If
  27.     Next
  28.    
  29. End With

  30. Application.Calculation = xlCalculationAutomatic
  31. Application.ScreenUpdating = True
  32. End Sub
複製代碼
懂得發問,答案就會在其中

今日の一秒は  明日にない
http://kimbalko-chi.blogspot.com
http://kimbalko.blogspot.com

TOP

回復 2# kimbal
謝謝kimbal.
但出現執行階段錯誤’13’: 型態不符合
偵錯在: With Sheets("總表").Cells(lngVlookupRow, lngCurrenMonth + 1) '放數據到總表上
learner

TOP

回復 1# yliu
有考慮直接用樞紐分析表嗎?
a.gif

TOP

回復 1# yliu

可以考慮用 SUMIFS 來代替 SUMPRODUCT, 因爲SUMIFS更有效率
ss

TOP

回復 3# yliu


    可否提供一下EXCEL?

另外也會建議用PIVOTTABLE 和SUMIFS
VBA LOOP很多情況也是比公式慢的.
懂得發問,答案就會在其中

今日の一秒は  明日にない
http://kimbalko-chi.blogspot.com
http://kimbalko.blogspot.com

TOP

回復  kimbal
謝謝kimbal.
但出現執行階段錯誤’13’: 型態不符合
偵錯在: With Sheets("總表").Cells( ...
yliu 發表於 2013-5-31 12:30

回復 2# kimbal
不好意思,我稍微添加了幾行,希望您等能笑納!
  1. Private Sub Ex()
  2.     Dim rowcnt As Long, i As Long
  3.     Dim lngVlookupRow As Long, lngCurrenMonth As Long, lngCurrValue As Long

  4.     Application.ScreenUpdating = False
  5.     Application.Calculation = xlCalculationManual

  6.     '  清理舊數據
  7.     Sheets("總表").Activate
  8.     Sheets("總表").Range("A3").CurrentRegion.Offset(1, 1).Clear


  9.     Sheets("原始資料").Activate
  10.     With Sheets("原始資料")
  11.         rowcnt = .Cells(1, 1).CurrentRegion.Rows.Count
  12.         For i = 2 To rowcnt
  13.             lngCurrenMonth = 0
  14.             If IsDate(.Cells(i, 2)) Then
  15.                 lngCurrenMonth = Month(.Cells(i, 2))                                               '  當行月份
  16.             End If
  17.             
  18.             If lngCurrenMonth > 0 And Not (IsError(Application.Match(.Cells(i, 1), Sheets("總表").Range("A:A"), 0))) Then
  19.                 lngVlookupRow = Application.Match(.Cells(i, 1), Sheets("總表").Range("A:A"), 0)    '  當行類型在總表行數
  20.                 lngCurrValue = .Cells(i, 3)
  21.                 With Sheets("總表")
  22.                     .Cells(lngVlookupRow, lngCurrenMonth + 1).Value = .Cells(lngVlookupRow, lngCurrenMonth + 1).Value + lngCurrValue                       '  放數據到總表上
  23.                     .Cells(lngVlookupRow, "N") = .Cells(lngVlookupRow, "N") + lngCurrValue
  24.                     .Cells(lngVlookupRow, Chr(79 + Int(lngCurrenMonth / 4))) = .Cells(lngVlookupRow, Chr(79 + Int(lngCurrenMonth / 4))) + lngCurrValue
  25.                 End With
  26.             End If
  27.         Next
  28.     End With
  29.    
  30.     With Sheets("總表")
  31.         rowcnt = .[A3].End(xlDown).Row
  32.         For i = 2 To 18
  33.             .Cells(rowcnt, i) = WorksheetFunction.Sum(.Range(Chr(64 + i) & 4 & ":" & Chr(64 + i) & (rowcnt - 1)))
  34.         Next i
  35.     End With
  36.    
  37.     Application.Calculation = xlCalculationAutomatic
  38.     Application.ScreenUpdating = True
  39. End Sub
複製代碼

TOP

本帖最後由 c_c_lai 於 2013-5-31 19:55 編輯

請將 4 改成 3.2
  1.                     .Cells(lngVlookupRow, Chr(79 + Int(lngCurrenMonth / 3.2))) = .Cells(lngVlookupRow, Chr(79 + Int(lngCurrenMonth /3.2))) + lngCurrValue
複製代碼

TOP

如附件:
兩個條件的加總.rar (20.85 KB)

TOP

回復 9# c_c_lai


   謝謝 c_c_lai,
   可以使用了, 感謝~
  再請問一下,   
.Cells(lngVlookupRow, Chr(79 + Int(lngCurrenMonth / 3.2))) = .Cells(lngVlookupRow, Chr(79 + Int(lngCurrenMonth / 3.2))) + lngCurrValue  '

除以3.2 的作用是什麼?
learner

TOP

        靜思自在 : 【時間如鑽石】時間對一個有智慧的人而言,就如鑽石般珍貴;但對愚人來說,卻像是一把泥土,一點價值也沒有。
返回列表 上一主題