excel - VBA Pivot Table -


i trying write vba build pivot table in new tab. when run code getting type mismatch error when creating pivottable cache.

the code below, appreciated...hopefully fresh @ can spot missing

sub makeapivottable()      dim ssheet, ssheet2 worksheet 'ssheet data is, ssheet2 pivot built     dim ccache pivotcache     dim pttable pivottable     dim rrange range     dim rlastrow, clastcolumn long      ' insert new sheet pivot table reside     application.displayalerts = false     sheets.add before:=activesheet     activesheet.name = "pivottable"     application.displayalerts = true     set ssheet2 = worksheets("pivottable")     set ssheet = worksheets("interactions db")      ' define range (the data want put pivot table     rlastrow = ssheet.cells(rows.count, 1).end(xlup).row     clastcolumn = ssheet.cells(1, columns.count).end(xltoleft).column     set rrange = ssheet.cells(1, 1).resize(rlastrow, clastcolumn)      ' create cache pivot table     set ccache = activeworkbook.pivotcaches.create _     (sourcetype:=xldatabase, sourcedata:=rrange). _     createpivottable(tabledestination:=ssheet2.cells(2, 2), _     tablename:="salespivottable")       ' insert blank table     set pttable = ccache.createpivottable _         (tabledestination:=ssheet2.cells(1, 1), tablename:="salespivottable")       'insert row fields     activesheet.pivottables("salespivottable").pivotfields("customer")     .orientation = xlrowfield     .position = 1     end       'insert column fields     'with activesheet.pivottables("salespivottable").pivotfields("interaction type")     '.orientation = xlcolumnfield     '.position = 1     'end      'insert data field     activesheet.pivottables("salespivottable").pivotfields("interactiontype")     .orientation = xldatafield     .position = 1     .function = xlsum     .numberformat = "#,##0"     .name = "person "     end       ' formatting       end sub 

try code below (explanations inside code comments)

option explicit  sub makeapivottable()      dim ssheet worksheet, ssheet2 worksheet  ' ssheet data is, ssheet2 pivot built     dim ccache pivotcache     dim pttable pivottable     dim rrange range     dim rlastrow long, clastcolumn long ' need define each 1 long, otherwise first 1 variant      ' insert new sheet pivot table reside     application.displayalerts = false      ' 2 lines below shorter version create new sheet,      ' assign ssheet2 , , name "pivottable"     on error resume next     set ssheet2 = sheets("pivottable") '<-- try set sheet (already created in past code runs)     on error goto 0      if ssheet2 nothing '<-- sheet not exist yet >> add         set ssheet2 = sheets.add(before:=activesheet)         ssheet2.name = "pivottable"     end if      application.displayalerts = true      set ssheet = worksheets("interactions db")      ' define range (the data want put pivot table     ssheet         rlastrow = .cells(.rows.count, 1).end(xlup).row         clastcolumn = .cells(1, .columns.count).end(xltoleft).column         set rrange = .cells(1, 1).resize(rlastrow, clastcolumn)     end      ' create cache pivot table ***** correct syntax     set ccache = activeworkbook.pivotcaches.create(sourcetype:=xldatabase, sourcedata:=rrange)      ' create new pivot table in ssheet2 sheet     set pttable = ssheet2.pivottables.add(pivotcache:=ccache, tabledestination:=ssheet2.range("a1"), tablename:="salespivottable")      pttable         'insert row fields         .pivotfields("customer")             .orientation = xlrowfield             .position = 1         end          'insert column fields         'with activesheet.pivottables("salespivottable").pivotfields("interaction type")         '.orientation = xlcolumnfield         '.position = 1         'end          'insert data field         .pivotfields("interactiontype")             .orientation = xldatafield             .position = 1             .function = xlsum             .numberformat = "#,##0"             .name = "person "         end          ' formatting     end  end sub 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -