- 帖子
- 2035
- 主題
- 24
- 精華
- 0
- 積分
- 2031
- 點名
- 0
- 作業系統
- Win7
- 軟體版本
- Office2010
- 閱讀權限
- 100
- 性別
- 男
- 註冊時間
- 2012-3-22
- 最後登錄
- 2024-2-1
|
22#
發表於 2013-6-2 10:45
| 只看該作者
回復 18# yliu
我將原本 Kimbal 版大提供的修正程式在予增修,
以及 sunnyso 前輩提供的程式略予修正後,寫成兩組
效益不錯的模組,供妳選用。
A. 之前提供的,改以陣列處理 (原本是對應實體欄位一一處理),
經修正後的程式碼:- Sub Ex_Match_Case() ' Match Case
- Dim RowsCnt As Long, i As Long, SubTotalAr() As Double
- Dim t1 As Variant, t2 As Variant, AllType As Variant
- Dim DataArea As Variant, Atype As Integer
- Dim lngCurrenMonth As Long, lngCurrValue As Long
- t1 = Timer
- Application.ScreenUpdating = False
- ' Application.Calculation = xlCalculationManual
- AllType = Array("A類", "B類", "C類", "D類", "E類", "F類", "G類", "H類", "I類", "J類")
- ReDim SubTotalAr(0 To UBound(AllType), 0 To 16)
- ' 清理舊數據
- Sheets("總表").Activate
- Sheets("總表").Range("A3").CurrentRegion.Offset(1, 1).Clear
- Sheets("原始資料").Activate
- With Sheets("原始資料")
- RowsCnt = .Cells(1, 1).CurrentRegion.Rows.Count
- DataArea = .Range("A2").Resize(RowsCnt - 1, 3)
-
- For i = 1 To UBound(DataArea)
- lngCurrenMonth = 0
- If IsDate(DataArea(i, 2)) Then
- lngCurrenMonth = Month(DataArea(i, 2)) ' 當行月份
- End If
-
- If lngCurrenMonth > 0 Then
- lngCurrValue = DataArea(i, 3)
-
- For Atype = 0 To UBound(AllType) ' A類 To J類
- If DataArea(i, 1) = AllType(Atype) Then ' Jan To Dec
- SubTotalAr(Atype, lngCurrenMonth - 1) = SubTotalAr(Atype, lngCurrenMonth - 1) + lngCurrValue
- SubTotalAr(Atype, 12) = SubTotalAr(Atype, 12) + lngCurrValue ' 累計
- SubTotalAr(Atype, 13 + Int(lngCurrenMonth / 3.2)) = SubTotalAr(Atype, 13 + Int(lngCurrenMonth / 3.2)) + lngCurrValue
- ' 除以 3.2:
- ' 1 (Int(0.3125)=0), 2 (Int(0.625))=0 ), 3 (Int(0.9375))=0 ), ' 第一季
- ' 4 (Int(1.25))=1), 5 (Int(1.5625))= 1),6 (Int(1.875 ))=1), ' 第二季
- ' 7 (Int(2.1875))= 2),8 (Int(2.5))=2 ), 9 (Int(2.8125))=2), ' 第三季
- ' 10(Int(3.125))=3), 11(Int(3.4375 ))=3),12(Int(3.75))=3) ' 第四季
- ' *************************************************************************************
- End If
- Next Atype
- End If
- Next i
- End With
-
- With Sheets("總表")
- .Range("B4").Resize(UBound(AllType) + 1, 17) = SubTotalAr
- ' RowsCnt = .[A3].End(xlDown).Row
- ' For i = 2 To 18
- ' .Cells(RowsCnt, i) = WorksheetFunction.Sum(.Range(Chr(64 + i) & 4 & ":" & Chr(64 + i) & (RowsCnt - 1))) ' 合計
- ' Next i
- .Range("B14:R14").FormulaR1C1 = "=SUM(R[-10]C:R[-1]C)"
- End With
-
- ' Application.Calculation = xlCalculationAutomatic
- Application.ScreenUpdating = True
- t2 = Timer
- ' MsgBox "耗時" & t2 - t1
- Sheets("原始資料").[F11] = "耗時: " & CDbl(t2 - t1)
- End Sub
複製代碼 B. 經測試,效益最佳的程式模組:- Sub Ex_VBA_Array() ' VBA Code DataArea Array
- Dim RowsCnt As Long, m As Long, SubTotalAr() As Double
- Dim t1 As Variant, t2 As Variant, AllType As Variant
- Dim DataArea As Variant
- Dim i%, j%
-
- t1 = Timer
- AllType = Array("A類", "B類", "C類", "D類", "E類", "F類", "G類", "H類", "I類", "J類")
- ReDim SubTotalAr(0 To UBound(AllType), 0 To 11)
- Application.ScreenUpdating = False
-
- ' 清理舊數據
- ' Sheets("總表").Activate
- Sheets("總表").Range("A3").CurrentRegion.Offset(1, 1).Clear
-
- With Sheets("原始資料")
- RowsCnt = .Range("A1").CurrentRegion.Rows.Count
- DataArea = .Range("A2").Resize(RowsCnt - 1, 3)
-
- For m = 1 To UBound(DataArea)
- For i = 0 To UBound(AllType) ' A類 To J類
- If DataArea(m, 1) = AllType(i) Then ' Jan To Dec
- SubTotalAr(i, Month(DataArea(m, 2)) - 1) = SubTotalAr(i, Month(DataArea(m, 2)) - 1) + DataArea(m, 3)
- End If
- Next i
- Next m
- End With
-
- With Sheets("總表")
- .Range("B4").Resize(UBound(AllType) + 1, 12) = SubTotalAr
- .Range("N4:N13").FormulaR1C1 = "=SUM(RC[-12]:RC[-1])"
- For i = 0 To 3
- ' .Range(Chr(79 + i) & 4 & ":" & Chr(79 + i) & 13).FormulaR1C1 = "=SUM(RC[-" & (13 - i * 2) & "]:RC[-" & (11 - i * 2) & "])"
- .Range(Chr(79 + i) & 4).Resize(UBound(AllType) + 1).FormulaR1C1 = "=SUM(RC[-" & (13 - i * 2) & "]:RC[-" & (11 - i * 2) & "])"
- .Range(Chr(79 + i) & 4).Resize(UBound(AllType) + 1) = .Range(Chr(79 + i) & 4).Resize(UBound(AllType) + 1).Value
- Next i
- .Range("B14:R14").FormulaR1C1 = "=SUM(R[-10]C:R[-1]C)"
- End With
- Application.ScreenUpdating = True
- t2 = Timer
- ' MsgBox "耗時" & t2 - t1
- Sheets("原始資料").[F3] = "耗時: " & CDbl(t2 - t1)
- End Sub
複製代碼 以上兩組模組,均使用 70 筆、以及 52,993 筆之資料 (sunnyso 前輩提供) 測試過。
妳可以自行測測看。 |
|