excel vba - I am unable to add Data Field to my Pivot Table VBA -


i runtime error

unable pivottables property of worksheet class

when run following code:

sub updatepivot()  dim ws worksheet, srcdata string, pvtcache pivotcache dim ws2 worksheet, nr long, nc long, ws3 worksheet dim pf pivotfield, pt pivottable, df pivotfield, str string  'set ws = thisworkbook.worksheets("lisun data") set ws2 = thisworkbook.worksheets("cover") set ws3 = thisworkbook.worksheets("stockist") set pt = ws3.pivottables("pivottable3") set pt = ws3.pivottables("pivottable3") pt.pivotfields(" may-17")     .orientation = xlcolumnfield     .function = xlsum     .position = 1 end  end sub 

may know wrong?

i did add data source data model beforehand, , i'm not sure causing error.

try code below try , trap errors, explanations inside code's comments:

option explicit  sub updatepivot()  dim ws worksheet, srcdata string, pvtcache pivotcache dim ws2 worksheet, nr long, nc long, ws3 worksheet dim pf pivotfield, pt pivottable, df pivotfield, str string  'set ws = thisworkbook.worksheets("lisun data") set ws2 = thisworkbook.worksheets("cover") set ws3 = thisworkbook.worksheets("stockist")  ' 1st: trap pivot-table object on error resume next set pt = ws3.pivottables("pivottable3") on error goto 0 if pt nothing '<-- pivot table does't exist (pivot table renamed ?)     msgbox "pivot-table object error!" else ' pivot-table object exists      ' 2ndt: trap pivotfield object     on error resume next     set pf = pt.pivotfields(" may-17")     on error goto 0     if pf nothing         msgbox "pivot-field object error!"     else         pf             .orientation = xlcolumnfield             .function = xlsum             .position = 1         end     end if end if  end sub 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -