Dynamic Criteria - Filtering Excel VBA -
i need filter on same criteria, values of criteria not same in data receive, need dynamic.
for eg.
dim crit1 string crit1 = ?
so that:
selection.autofilter field:=4, criteria1:=crit1
here code
enter code herexdim tasktype, status, elapse long dim total, completed variant total = array("completed", "error", "killed") completed = array("completed") tasktype = worksheetfunction.match("tasktypeid", rows("1:1"), 0) status = worksheetfunction.match("status", rows("1:1"), 0) elapse = worksheetfunction.match("elapse", rows("1:1"), 0) 'use filter criteria '100 total sheets("raw_data") set rndata = .usedrange rndata .autofilter field:=tasktype, criteria1:="100" .autofilter field:=status, criteria1:=total, operator:=xlfiltervalues .select each rngarea in .specialcells(xlcelltypevisible).areas lcount = lcount + rngarea.rows.count next = lcount - 1 end end '100 completed sheets("data") set rndata = .usedrange rndata .autofilter field:=tasktype, criteria1:="100" .autofilter field:=status, criteria1:=completed, operator:=xlfiltervalues .select each rngarea in .specialcells(xlcelltypevisible).areas lcount1 = lcount1 + rngarea.rows.count next b = lcount1 - 1 end end '101 total sheets("raw_data") set rndata = .usedrange rndata .autofilter field:=tasktype, criteria1:="101" .autofilter field:=status, criteria1:=total, operator:=xlfiltervalues .select each rngarea in .specialcells(xlcelltypevisible).areas lcount2 = lcount2 + rngarea.rows.count next c = lcount2 - 1 end end '101 completed sheets("data") set rndata = .usedrange rndata .autofilter field:=tasktype, criteria1:="101" .autofilter field:=status, criteria1:=completed, operator:=xlfiltervalues .select each rngarea in .specialcells(xlcelltypevisible).areas lcount3 = lcount3 + rngarea.rows.count next d = lcount3 - 1 end end in above code have calculated criteria1:= 100,101 statically should take dynamic value present in filter. in advance.
for example, using macro recorder filter column "i" items contain "a", kind of code.
sub macro5() ' ' macro5 macro ' ' columns("i:i").select selection.autofilter activesheet.range("$i$1:$i$7").autofilter field:=1, criteria1:="=*a*", _ operator:=xland end sub now see how code uses wildcards , can edit code use wildcards , variable. new code this.
sub filterfora() dim s string s = "a" columns("i:i").autofilter field:=1, criteria1:="=*" & s & "*" end sub
Comments
Post a Comment