標題:
[發問]
請問如何將這句+{sum(if......,if......,))}, 用在Macro裡面??
[打印本頁]
作者:
idsmchow
時間:
2011-12-24 00:29
標題:
請問如何將這句+{sum(if......,if......,))}, 用在Macro裡面??
一個Workbook裡面有兩將Sheets, 分別名為"Report" & "Data".
當三個條件Match 的情況下, 就將Amount 加起來.
請問如何將這句+{sum(if......,if......,))}, 用在Macro裡面??
作者:
register313
時間:
2011-12-24 11:27
回復
1#
idsmchow
初學者VBA
Sub xx()
Set Rng1 = Sheets("Data").Columns("A:C")
Set Rng2 = Sheets("Report").Range("A1:B2")
C = 10
Do Until Sheets("Report").Cells(C, 2) = ""
If Sheets("Report").Cells(C, 1) = "" Then
Sheets("Report").Range(Cells(C, 5), Cells(C, 6)) = 0
End If
If Sheets("Report").Cells(C, 1) = "Y" Then
Rng2.Cells(1, 1) = "Code"
Rng2.Cells(1, 2) = "Type"
Rng2.Cells(2, 1) = Sheets("Report").Cells(C, 2)
Rng2.Cells(2, 2) = "A"
Sheets("Report").Cells(C, 5) = WorksheetFunction.DSum(Rng1, 3, Rng2)
Rng2.Cells(2, 2) = "B"
Sheets("Report").Cells(C, 6) = WorksheetFunction.DSum(Rng1, 3, Rng2)
End If
C = C + 1
Loop
Rng2.Clear
End Sub
複製代碼
[attach]8917[/attach]
作者:
GBKEE
時間:
2011-12-24 17:43
本帖最後由 GBKEE 於 2011-12-24 17:46 編輯
回復
1#
idsmchow
自動篩選
Option Explicit
Sub Ex()
Dim C As Integer
C = 10
With Sheets("Report")
Do Until .Cells(C, "B") = ""
If .Cells(C, "A") = "" Then
.Range(.Cells(C, "E"), .Cells(C, "G")) = 0
ElseIf .Cells(C, "A") = "Y" Then
Sheets("Data").Cells.AutoFilter
Sheets("Data").Cells.AutoFilter 1, "A"
Sheets("Data").Cells.AutoFilter 2, .Cells(C, "B")
.Cells(C, "E") = Application.Sum(Sheets("Data").Columns(3).SpecialCells(xlCellTypeVisible))
Sheets("Data").Cells.AutoFilter
Sheets("Data").Cells.AutoFilter 1, "B"
Sheets("Data").Cells.AutoFilter 2, .Cells(C, "B")
.Cells(C, "F") = Application.Sum(Sheets("Data").Columns(3).SpecialCells(xlCellTypeVisible))
.Cells(C, "G") = .Cells(C, "E") + .Cells(C, "F")
End If
C = C + 1
Loop
End With
Sheets("Data").AutoFilterMode = False
End Sub
複製代碼
作者:
Hsieh
時間:
2011-12-25 10:28
本帖最後由 Hsieh 於 2011-12-25 16:13 編輯
回復
1#
idsmchow
這個問題在2007版本以後就很簡單用SUMIFS函數錄製巨集即可
2003版就寫個多條件加總函數來運用
一般模組
Function SumIIF(SumRange As Range, ParamArray OtherArgs())'多條件加總函數,可直接運用到儲存格
'語法:SumIIF(SumRange,準則1,準則範圍1,準則2,準則範圍2,準則3,準則範圍3...準則n,準則範圍n)
'準則範圍大小必須與SumRange範圍大小相同
Dim mystr$, temp$
Set dic = CreateObject("Scripting.Dictionary")
For i = LBound(OtherArgs) To UBound(OtherArgs) Step 2
If mystr = "" Then mystr = OtherArgs(i) Else mystr = mystr & OtherArgs(i)
dic(i + 1) = ""
Next
For i = 1 To SumRange.Count
For Each ky In dic.keys
If temp = "" Then temp = OtherArgs(ky)(i) Else temp = temp & OtherArgs(ky)(i)
Next
If temp = mystr Then SumIIF = SumIIF + SumRange(i)
temp = ""
Next
End Function
Sub ex()'主程式
Dim Sr As Range
With Sheets("Data")
Set Sr = .Range("C2", .[C2].End(xlDown))
Set cr1 = .Range("A2").Resize(Sr.Count, 1)
Set cr2 = .Range("B2").Resize(Sr.Count, 1)
End With
With Sheets("Report")
For Each c In .[E8:F8]
For Each a In .Range("A:A").SpecialCells(xlCellTypeConstants)
If a = "Y" Then .Cells(a.Row, c.Column) = SumIIF(Sr, c, cr1, a.Offset(, 1), cr2)
Next
Next
End With
End Sub
複製代碼
[attach]8922[/attach]
作者:
idsmchow
時間:
2011-12-26 00:44
回復
3#
GBKEE
謝謝(register313, GBKEE, Hsieh)各位回覆.
另外還有一個問題
.Cells(C, "G") = .Cells(C, "E") + .Cells(C, "F") ‘這句語法能夠將Column 的Total 加起來.
至於 Row 的Total 又如何做法? (Sub Total & Grand Total)
因為A/C Code 有可能增加 / 減少, 資料範圍隨時會變動.
例如:
A/C CODE
101-1
101-2
101-3
101-4
Sub-Total
215-2018
215-2019
Sub-Total
215-3022
215-3023
215-3024
215-3025
215-3026
Sub-Total
Grand Total
作者:
register313
時間:
2011-12-26 17:13
回復
5#
idsmchow
初學者VBA
Sub aa()
LastCol = ActiveSheet.UsedRange.SpecialCells(xlLastCell).Column
LastRow = ActiveSheet.UsedRange.SpecialCells(xlLastCell).Row
For C = 2 To LastCol - 1
X = 1
For R = 2 To LastRow
If Cells(R, 1) = "Sub Total" Then
Cells(R, C) = Application.Sum(Cells(X + 1, C).Resize(R - X - 1, 1))
X = R
End If
Cells(R, LastCol) = Application.Sum(Cells(R, 2).Resize(1, LastCol - 2))
Next R
Cells(LastRow, C) = Application.Sum(Cells(2, C).Resize(LastRow - 2, 1)) / 2
Next C
End Sub
複製代碼
[attach]8924[/attach]
作者:
idsmchow
時間:
2011-12-27 00:25
回復
6#
register313
Thank
可否解釋 LastCol - 1, Cells(R, C) = Application.Sum(Cells(X + 1, C).
Resize
(R - X - 1, 1)) X = R, 這兩句?
謝謝耐心教導
作者:
register313
時間:
2011-12-27 08:30
回復
7#
idsmchow
Cells(X + 1, C).Resize(R - X - 1, 1)
'以前面之單元儲存格為基準,擴大儲存格範圍之列數與行數
'以Cells(X + 1, C)單元儲存格為基準,擴大儲存格範圍為R - X - 1列與1行
Application.Sum (Cells(X + 1, C).Resize(R - X - 1, 1))
'儲存格範圍作加總
作者:
idsmchow
時間:
2011-12-27 23:52
Thank
明白了
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)