DÙNG VBA ĐIỀU KHIỂN PIVOT
TABLE ĐỂ TẠO BÁO CÁO
I. Điều khiển các chức năng cơ bản và nâng cao:
1. Hoán đổi vị trí các fields trong cùng Area
PHP Code:
With ActiveSheet.PivotTables("<PivotTableName>")
.PivotFields("<FieldName>").Position = IIf(.Position = 1, 2, 1)
' Dời Row field qua trái hoặc Column Field lên trên:'
.PivotFields("<FieldName1>").Position = .Position - 1
' Dời Row field qua phải hoặc Column Field xuống dưới:'
.PivotFields("<FieldName2>").Position = .Position + 1
End With
2. Chuyển vị trí fields từ Area này qua Area khác
PHP Code:
With ActiveSheet.PivotTables("<PivotTableName>")
.PivotFields("<FieldName1>").Orientation = xlRowField 'Row Area'
.PivotFields("<FieldName2>").Orientation = xlColumnField 'Column Area'
.PivotFields("<FieldName3>").Orientation = xlPageField 'Page Area'
End With
3. Thêm & thay đổi dữ liệu cần phần tích trong Data Area
PHP Code:
With ActiveSheet.PivotTables("<PivotTableName>")
.PivotFields("Alias of <FieldName1>").Orientation = xlHidden 'gỡ field ra kh
ỏi Data Area'
.AddDataField ActiveSheet.PivotTables("<PivotTableName>").PivotFields("
<FieldName2>"), _
"Alias of <FieldName2>", xlSum/ xlMax/ xlMin/ 'Thêm field vào Data Ar
ea'
End With
4. Show - Hide GrandTotal
PHP Code:
With ActiveSheet.PivotTables("PivotTable1")
.RowGrand = True/ False
.ColumnGrand = True/ False
End With
5. Filter single selection
PHP Code:
ActiveSheet.PivotTables("<PivotTableName>").PivotFields("<FieldName>").Cur
rentPage = <FieldItem>
6. Filter Multiple selection:
PHP Code:
With ActiveSheet.PivotTables("<PivotTableName>").PivotFields("<FieldName>"
)
For i = 1 To .PivotItems.Count
With .PivotItems(i)
.Visible = (Condition1 And/Or Condition2 And/Or Condition3 )
End With
Next
End With
7. Thay đổi field setting
PHP Code:
With ActiveSheet.PivotTables("PivotTable1").PivotFields(CurrentField)
' Sum:'
.Calculation = xlSum
'Max:'
.Calculation = xlMax
'Mức tăng giảm so với kỳ trước:'
.Calculation = xlDifferenceFrom
.BaseField = "Nam"
.BaseItem = "(previous)"
.NumberFormat = "#,###"
'Mức tăng giảm so với kỳ gốc (2000):'
.Calculation = xlDifferenceFrom
.BaseField = "Nam"
.BaseItem = "2000"
.NumberFormat = "#,###"
'Tỷ lệ tăng giảm so với kỳ trước:'
.Calculation = xlPercentDifferenceFrom
.BaseField = "Nam"
.BaseItem = "(previous)"
'Tỷ lệ tăng giảm so với kỳ gốc (2000):'
.Calculation = xlPercentDifferenceFrom
.BaseField = "Nam"
.BaseItem = "2000"
.NumberFormat = "0.00%"
'Tỷ lệ thành phần trong cột:'
.Calculation = xlPercentOfColumn
'Tỷ lệ thành phần trong hàng:'
.Calculation = xlPercentOfRow
'Tỷ lệ thành phần trong cả bảng:'
.Calculation = xlPercentOfTotal
'Excel 2010:Tỷ lệ thành phần so với subtotal Dòng (parent Row)'
.Calculation = xlPercentOfParentRow
'Excel 2010:Tỷ lệ thành phần so với subtotal cột (parent Column)'
.Calculation = xlPercentOfParentColumn
End With
8. Group trường ngày tháng:
PHP Code:
'Group theo tháng:'
[C16].Group Start:=True, End:=True, Periods:=Array(False, False, False _
, False, True, False, False)
'Group theo quý'
[C16].Group Start:=True, End:=True, Periods:=Array(False, False, False, _
False, False, True, False)
'Group theo quý và tháng (2 cấp)'
[C16].Group Start:=True, End:=True, Periods:=Array(False, False, False, _
False, True, True, False)
'Group theo 6 tháng (tức group theo ngày, 183 ngày)'
[C16].Group Start:=39448, End:=39813, By:=183, Periods:=Array(False, _
False, False, True, False, False, False)
II. Sử dụng các control của Form và của Control toolbox:
- OptionButton
- Groupbox
- Checkbox
- Toggle Button
- Combobox (Form Control)
- Combobox (ActiveX control)
- Command Button