- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
2#
發表於 2015-7-9 08:57
| 只看該作者
回復 1# HSINLI
可修改如下- For i = 2 To 交.Range("d2").End(xlDown).Row
- If 交.Range("c" & i).RowHeight Then 'RowHeight > 0 '儲存格不是隱藏的
- If 交.Range("c" & i) = "Bought" Then
- po.Range("c" & ipo) = po.Range("c" & ipo) + 交.Range("e" & i)
- ElseIf 交.Range("c" & i) = "Sold" Then
- po.Range("c" & ipo) = po.Range("c" & ipo) - 交.Range("e" & i)
- End If
- End If
- Next i
複製代碼 也可如此- Option Explicit
- Sub Ex()
- Dim po As Worksheet, 交 As Worksheet
- Dim ipo As Integer, Bought As Integer, Sold As Integer
- Set po = Sheets("position")
- Set 交 = Sheets("交易紀錄")
- Application.ScreenUpdating = False
- po.Range("a5", po.[A5].End(xlDown)) = ""
- 'AdvancedFilter xlFilterCopy 在指定的其他範圍的欄位(無字串,複製全部欄位。有字串,須為資料庫的欄位)
- 交.ListObjects("表格1").Range.Range("D:D").AdvancedFilter xlFilterCopy, , po.Range("a5"), True
-
- 'AdvancedFilter(進階篩選) ,AutoFilter(自動篩選)
- 'AdvancedFilter Action:= xlFilterInPlace 在篩選的範圍顯示篩選後的資料
- With po
- For ipo = 6 To .Range("a5").End(xlDown).Row '
- 交.ListObjects("表格1").Range.AutoFilter field:=4, Criteria1:=.Range("a" & ipo)
- With 交.ListObjects("表格1").Range
- .AutoFilter field:=3, Criteria1:="Bought"
- Bought = Application.Sum(.Range("E:E").SpecialCells(xlCellTypeVisible))
- 'Application.Sum 工作表函數
- '.SpecialCells [特殊的儲存格] (xlCellTypeVisible) 參數:=可見的儲存格
- .AutoFilter field:=3, Criteria1:="Sold"
- Sold = Application.Sum(.Range("E:E").SpecialCells(xlCellTypeVisible))
- End With
- .Cells(ipo, "C") = Bought - Sold
- Next
- End With
- 交.ListObjects("表格1").Range.AutoFilter '沒有準則 = 取消自動篩選
- Application.ScreenUpdating = True
- End Sub
複製代碼 |
|