excel - VBA Run Time Error 1004 AutoFilter method of Range class Failed -
i hope can help. getting error run time error 1004 autofilter method of range class failed
when run code public sub testthis()
, funny thing works itself, when put other code , call it, gives error run time error 1004 autofilter method of range class failed
the error happening on line
.range("a:k").autofilter field:=11, criteria1:="<>", operator:=xlfiltervalues
like said when not called , run no problem when called bugs. appreciated.
my code below.
sub open_workbook_dialog() dim my_filename variant dim my_workbook workbook msgbox "pick cro file" '<--| txt box prompt pick file my_filename = application.getopenfilename(filefilter:="excel files,*.xl*;*.xm*") '<--| opens file window allow selection if my_filename <> false set my_workbook = workbooks.open(filename:=my_filename) call testthis call filter(my_workbook) '<--|calls filter code , executes end if end sub public sub filter(my_workbook workbook) dim rcountry range, helpcol range dim wb workbook my_workbook.sheets(1) '<--| refer data worksheet .usedrange set helpcol = .resize(1, 1).offset(, .columns.count) '<--| "helper" column @ right of used range, it'll used store unique country names in end .range("a1:y" & .cells(.rows.count, 1).end(xlup).row) '<--| refer columns "a:y" row 1 last non empty row of column "a" .columns(11).advancedfilter action:=xlfiltercopy, copytorange:=helpcol, unique:=true '<-- call advancedfilter on 11th column of referenced range , store unique values in "helper" column set helpcol = range(helpcol.offset(1), helpcol.end(xldown)) '<--| set range unique names in (skip header row) each rcountry in helpcol '<--| iterate on unique country names range (skip header row) .autofilter 11, rcountry.value2 '<--| filter data on country field (11th column) current unique country name if application.worksheetfunction.subtotal(103, .cells.resize(, 1)) > 1 '<--| if cell other header ones has been filtered... set wb = application.workbooks.add '<--... add new workbook wb.saveas filename:=rcountry.value2 '<--... saves workbook after country .specialcells(xlcelltypevisible).copy wb.sheets(1).range("a1") activesheet.name = rcountry.value2 '<--... rename .specialcells(xlcelltypevisible).copy activesheet.range("a1") 'copy data country under header sheets(1).range("a1:y1").wraptext = false 'takes wrap text off activewindow.zoom = 55 sheets(1).usedrange.columns.autofit 'autofits column wb.close savechanges:=true '<--... saves , closes workbook end if next end .autofiltermode = false '<--| remove autofilter , show rows end helpcol.offset(-1).end(xldown).clear '<--| clear helper column (header included) end sub public sub testthis() dim wks worksheet set wks = thisworkbook.sheets(1) wks .autofiltermode = false .range("a:k").autofilter field:=11, criteria1:="<>", operator:=xlfiltervalues .range("a:c").specialcells(xlcelltypeblanks).interior.color = 65535 .autofiltermode = false end end sub
please check if set wks = thisworkbook.sheets(1)
gives sheet want, from workbook want. thisworkbook.
statement refers workbook, macro (vba project) placed. maybe need change
set wks = activeworkbook.sheets(1)
or pass my_workbook
testthis()
macro.
Comments
Post a Comment