create an excel pivot table report using vba

Upload: tranhungdao12a3

Post on 07-Aug-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/21/2019 Create an Excel Pivot Table Report Using VBA

    1/8

    Create an Excel Pivot Table report using VBA Related Links:1. Create an Excel Pivot Table report using VBA

    2. Referencing an Excel Pivot Table Range using VBA3. Reference Pivot Fiels an Pivot !te"s in an Excel Pivot Table# using VBA

    $. Excel Pivot Table Aress# %ocation# &ove ' Cop( using VBA

    ). Excel Pivot Table %a(out an *esign# using VBA+. Excel Pivot Table Properties ' ,ettings# using VBA

    -. Refres Excel Pivot Table an Cace of PivotTable# using VBA

    /. Excel Pivot Tables 0rouping 0roup !te"s# 0roup *ata an 0roup *ate Values# using VBA

    . Excel Pivot Tables ,ort Fiels# Values ' *ates# use Custo" %ists# it VBA14. Excel Pivot Tables Filter *ata# !te"s# Values ' *ates using VBA

    11. Excel Pivot Tables ,u""ar( Functions# Custo" Calculations ' Value Fiel ,ettings# using VBA12. Excel Pivot Tables !nsert Calculate Fiels ' Calculate !te"s# Create For"ulas using VBA

    13. Create ' Custo"i5e Excel Pivot Table Carts# using VBA

    Refer complete Tutorial on using Pivot Tables in Excel indo! "user interface#:

    Create an Custo"i5e a Pivot Table report

    $se Visual Basic for Applications "VBA# to Create and Customi%e PivotTable & PivotC'art reports:  A

    PivotTable report can be create# custo"i5e an "anipulate entirel( using VBA. Properties an &etos of tPivotTable ob6ect are use to create# custo"i5e an "anipulate a PivotTable report.

     !t "a( be note tat tere are ifferences beteen Pivot Tables create in Excel 2444# Excel 2442743# Excel 24an Excel 2414. 8ence it is i"portant to eter"ine in ic version of Excel is vba being execute in# to ensur

    co"patibilit( an si"ilar efault settings beteen te PivotTable it te corresponing Excel version.

     

    9ou can create a PivotTable it a specific version in Excel 244- b( using te PivotCaces.Create &eto. 

    !n Excel 244-# (ou can ave full( interactive PivotTables versions 4 to 3. !n Excel versions prior to Excel 244-

    (ou can ave full( interactive PivotTable version nu"bers 4 to 2 :ie. Excel 2444 ' Excel 2442743 for"ats;# buonl( a rea7onl( PivotTable version nu"ber 3 :ie. Excel 244- for"at; an (ou cannot create a PivotCart fro"

    rea7onl( PivotTable.

     nce te PivotTable is upgrae an converte toversion 3# it cannot be converte bac= to an earlier version.

     

    All features of Excel 244- can be use en (ou use a PivotTable version nu"ber 3 :ie. Excel 244- for"at; inExcel 244-. ?sing PivotTable version nu"bers 4 to 2 :ie. Excel 2444 ' Excel 2442743 for"ats; in Excel 244-

    enable use of "ost but not all features :esp. so"e filtering options; of Excel 244-.

     

    !n te orer of PivotTable Version( PivotTable Version )umber( Excel Version( Excel Version )umber 

    xlPivotTableVersion2444# 4# Excel 2444# .4xlPivotTableVersion14# 1# Excel 2442# 14.4

    xlPivotTableVersion11# 2# Excel 2443# 11.4

    xlPivotTableVersion12# 3# Excel 244-# 12.4

    xlPivotTableVersion1$# $# Excel 2414# 1$.4 @ote Tere is no ifference in beavior beteen xlPivotTableVersion14 an xlPivotTableVersion11.

     

    Application.Version Propert( 7 returns te Excel version nu"ber in ic VBA is being execute vi5. 14.4# 11.12.4# etc.

     

    http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=154:referencing-an-excel-pivot-table-range-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=156:reference-pivot-fields-and-pivot-items-in-an-excel-pivot-table-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=155:excel-pivot-table-address-location-move-a-copy-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=149:excel-pivot-table-layout-and-design-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=158:excel-pivot-table-properties-a-settings-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=159:refresh-excel-pivot-table-and-cache-of-pivottable-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=153:excel-pivot-tables-grouping-group-items-group-data-and-group-date-values-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=148:excel-pivot-tables-sort-fields-values-a-dates-use-custom-lists-with-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=150:excel-pivot-tables-filter-data-items-values-a-dates-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=151:excel-pivot-tables-summary-functions-custom-calculations-a-value-field-settings-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=152:excel-pivot-tables-insert-calculated-fields-a-calculated-items-create-formulas-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=157:create-a-customize-excel-pivot-table-charts-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=135:create-an-excel-pivot-table-report-pivottable-field-list&catid=81&Itemid=486http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=156:reference-pivot-fields-and-pivot-items-in-an-excel-pivot-table-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=155:excel-pivot-table-address-location-move-a-copy-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=149:excel-pivot-table-layout-and-design-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=158:excel-pivot-table-properties-a-settings-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=159:refresh-excel-pivot-table-and-cache-of-pivottable-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=153:excel-pivot-tables-grouping-group-items-group-data-and-group-date-values-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=148:excel-pivot-tables-sort-fields-values-a-dates-use-custom-lists-with-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=150:excel-pivot-tables-filter-data-items-values-a-dates-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=151:excel-pivot-tables-summary-functions-custom-calculations-a-value-field-settings-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=152:excel-pivot-tables-insert-calculated-fields-a-calculated-items-create-formulas-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=157:create-a-customize-excel-pivot-table-charts-using-vba&catid=79&Itemid=475http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=135:create-an-excel-pivot-table-report-pivottable-field-list&catid=81&Itemid=486http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=154:referencing-an-excel-pivot-table-range-using-vba&catid=79&Itemid=475

  • 8/21/2019 Create an Excel Pivot Table Report Using VBA

    2/8

    PivotTable.Version Propert( 7 returns te PivotTable version nu"ber# 1# 2# 3# etc.

    7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777

    Example *:$se t'e PivotCac'es+Create ,et'od( to add a ne! PivotTable Cac'e "version excel -../#+ Create a

    PivotTable report based on a Pivot Cac'e using t'e PivotCac'e+CreatePivotTable met'od+ Add ro!( colu

    page "report filter# & data fields+

    0mage *

    1ub createPivotTable*"#refer !"age *ata,ource for source ata. @ote tat te co"plete ata table extens upto ro nu"ber $.

    refer !"age 1 for PivotTable report create after running belo coe

    *i" PvtTbl As PivotTable

    *i" s*ata As

  • 8/21/2019 Create an Excel Pivot Table Report Using VBA

    3/8

    VersionxlPivotTableVersion12;.createPivotTable Table*estinationsPvtTbl.Range:A1;#

    Table@a"ePivotTable1# *efaultVersionxlPivotTableVersion12 

    ,et PvtTbl sPvtTbl.PivotTables:PivotTable1;

     *efault value of &anual?pate propert( is False erein a PivotTable report is recalculate auto"aticall( on

    eac cange. Turn off auto"atic upation of Pivot Table uring te process of its creation to spee up coe.

    PvtTbl.&anual?pate True

    a ro# colu"n an page :report filter; fiels

    ,et pvtFl PvtTbl.PivotFiels:9ear; pvtFl.>rientation xlPageFiel

    ,et pvtFl PvtTbl.PivotFiels:Region;

     pvtFl.>rientation xlRoFiel

    ,et pvtFl PvtTbl.PivotFiels:Car &oels;

     pvtFl.>rientation xlRoFiel pvtFl.Position 1

    ,et pvtFl PvtTbl.PivotFiels:Countr(;

     pvtFl.>rientation xlColu"nFiel

    set ata fiel 7 specificall( cange orientation to a ata fiel an set its function propert(

    rientation xl*ataFiel

    .Function xl,u"

    .@u"berFor"at #4

    .Position 1

    En

  • 8/21/2019 Create an Excel Pivot Table Report Using VBA

    4/8

    0mage - 

    1ub createPivotTable-"#

    refer !"age *ata,ource for source ata. @ote tat te co"plete ata table extens upto ro nu"ber $.

    refer !"age 2 for PivotTable report create after running belo coe

    *i" PvtTbl As PivotTable

    *i" s*ata As

  • 8/21/2019 Create an Excel Pivot Table Report Using VBA

    5/8

    to vie te PivotTable in Classic Pivot Table %a(out# set !n0ri*ropIones propert( to True# else set to False

    PvtTbl.!n0ri*ropIones False

    *efault value of &anual?pate propert( is False erein a PivotTable report is recalculate auto"aticall( on

    eac cange. Turn off auto"atic upation of Pivot Table uring te process of its creation to spee up coe.PvtTbl.&anual?pate True

    A ro# colu"n an page fiels in a Pivot Table using te AFiels "eto

    a ro# colu"n an page :report filter; fiels using te .AFiels "eto7PvtTbl.AFiels RoFielsArra(:Car &oels# Region;# Colu"nFielsCountr(# PageFiels9ear

    using te A*ataFiel "eto to a a ata fiel in a PivotTable reportTis "eto inclues 3 argu"ents of Fiel# Caption ' ,u""ar( Function.

  • 8/21/2019 Create an Excel Pivot Table Report Using VBA

    6/8

    0mage 2

    1ub createPivotTable2"#

    refer !"age *ata,ource for source ata. @ote tat te co"plete ata table extens upto ro nu"ber $.

    refer !"age 3 for PivotTable report create after running belo coe

    *i" PvtTbl As PivotTable

    *i" s*ata As

  • 8/21/2019 Create an Excel Pivot Table Report Using VBA

    7/8

    set ata fiel 7 specificall( cange orientation to a ata fiel an set its function propert(rientation xl*ataFiel

    .Function xl,u"

    .@u"berFor"at #4

    .Position 1

    En "itting ,ourceT(pe ill efault toxl*atabase :ie. Excel %ist or *atabase;D ,ource*ata# toug optional# soul invariabl( be specifie else Excel

    consiers te source to be a na"e range *atabase an if tis na"e range oes not exist te "eto ill fai

    certain conitions are not "et. !f ,ource*ata is specifie ten ,ourceT(pe soul also be specifie. >"itting tTable*estination argu"ent ill place te report at te active cell# an if te active cell lies itin te range of

    ,ource*ata# te report gets place eiter in a ne or=seet or in te separate or=seet specifie in te "eto b( te b6ectVariable.

    Example 5:Create a PivotTable report using t'e PivotTablei%ard ,et'od+ Add ro!( column and page "report filter

    fields using t'e Add3ields met'od+ Add a data field using t'e Add4ata3ield met'od+

    0mage 5

     

  • 8/21/2019 Create an Excel Pivot Table Report Using VBA

    8/8

    1ub createPivotTable5"#

    refer !"age *ata,ource for source ata. @ote tat te co"plete ata table extens upto ro nu"ber $.refer !"age $ for PivotTable report create after running belo coe

    *i" PvtTbl As PivotTable*i" s*ata As