excel - Looping Through Filters and Saving as PDF -
i have excel file contains list of models , owners. have been trying filter owner (column j) , print out models own in pdf. however, when run code below presents me "run time error 1004 autofilter method or range failed" on line
rngtofilter.autofilter field:=rngtofilter.column, criteria1:=filtercell.value
as new vba user, unsure of coming from. identifying error appreciated!
sub filtermacro() dim wb workbook dim ws worksheet set wb = activeworkbook set ws = sheets("consolidatedlist") ws.select dim filtercol string: filtercol = "j" 'the column containing data filter dim headerrow string: headerrow = "1" 'the row containing headers data 'this location file saved to. saves desktop. 'if necessary, change savepath desired location. make sure include ending \ dim savepath string: savepath = environ("userprofile") & "\desktop\" 'the range of cells filter looping through dim rngtofilter range: set rngtofilter = range(filtercol & headerrow, cells(rows.count, filtercol).end(xlup)) 'readying variables loop dim filtercell range dim appliedfilters() string dim filtercount long, long 'disables screenupdating allow code run faster , prevent screen flickering application.screenupdating = false 'starts loop each cell in rngtofilter each filtercell in rngtofilter 'checks if cell header if filtercell.row <> headerrow 'found not header, checks if filter cell's value has been applied dim alreadyfiltered boolean: alreadyfiltered = false = 1 filtercount if appliedfilters(i) = filtercell.value alreadyfiltered = true exit end if next if alreadyfiltered = false 'found filter cell's value has not yet been applied 'filters data rngtofilter.autofilter field:=rngtofilter.column, criteria1:=filtercell.value 'updates array containing applied filters filtercount = filtercount + 1 redim preserve appliedfilters(1 filtercount) appliedfilters(filtercount) = filtercell.value 'saves filtered data pdf activesheet.exportasfixedformat _ type:=xltypepdf, _ filename:=savepath & filtercell.value & ".pdf", _ quality:=xlqualitystandard, _ includedocproperties:=true, _ ignoreprintareas:=false, _ openafterpublish:=false end if end if next filtercell 'removes filter rngtofilter.autofilter 'enables screenupdating application.screenupdating = true end sub
wiki
Comments
Post a Comment