- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
4#
發表於 2013-8-21 14:06
| 只看該作者
回復 3# jackyliu - Sub Ex()
- Dim D_1, D_2, R As Range, M As Integer
- With Sheets("TEST").PivotTables(1) '工作表的幣一個,樞紐分析表.
- .Parent.Cells.Interior.ColorIndex = xlNone
- With .ColumnRange
- .Cells(1, .Columns.Count + 1).EntireColumn.Clear '樞紐分析表的欄位的最右邊的下一欄: 整欄 全部清除
- End With
- Application.DisplayAlerts = False
- .SourceData = Sheets("RAW").Range("A1").CurrentRegion.Address(, , xlR1C1, 1)
- '更新資料庫的位置 CurrentRegion不會包含有空白的資料
- '原先資料庫的位置=> A:D 的整欄包含有空白的資料
- Application.DisplayAlerts = True
- .PivotCache.Refresh
- With .ColumnRange '樞紐分析表的欄位
- M = .Cells(1, .Columns.Count + 1).Column '樞紐分析表的欄位的最右邊的下一欄
- D_1 = CDate(Application.Large(.Rows(2), 1)) '最大的日期
- D_2 = CDate(Application.Large(.Rows(2), 2)) '次一日的日期
- D_1 = .Rows(2).Find(D_1, LookIn:=xlValues).Column '最大日期的欗號
- D_2 = .Rows(2).Find(D_2).Column '次一日的欗號
- End With
- Set R = .RowRange.Columns(1).Cells(2)
- Do While Not Intersect(R, .RowRange.Columns(1)) Is Nothing
- If InStr(R, "合計") Then
- R.Resize(, .RowRange.Columns.Count + .ColumnRange.Columns.Count).Interior.Color = vbYellow
- End If
- With .Parent '樞紐分析表的父層 :工作表
- .Cells(R.Row, M) = .Cells(R.Row, D_1) - .Cells(R.Row, D_2)
- End With
- Set R = R.Offset(1)
- Loop
- End With
- End Sub
複製代碼 |
|