excel - VBA filter result row count wrong -
excel - VBA filter result row count wrong -
language: excel vba
scenario: have source range (rngdtref_allrecord) need insert info destination range (rngrtdc_alldetail)
for each of row(rngcurrrow) in source range (rngdtref_allrecord), filter destination range (rngrtdc_alldetail)
if filter yield result, add together info result row (note: each of result unique)
else add together new row destination range (rngrtdc_alldetail)
below code:
each rngcurrrow in rngdtref_allrecord.rows intrtdc_rowbegin = 7 intrtdc_colidxtotal = 20 intrtdc_rowlast = fntgetnewlastrow 'this function lastly row of rngrtdc_alldetail due might add together in new row set rngrtdc_alldetail = shtrtdc.range(shtrtdc.cells(intrtdc_rowbegin, 1), shtrtdc.cells(intrtdc_rowlast, intrtdc_colidxtotal)) rngrtdc_alldetail.autofilter rngrtdc_alldetail.autofilter field:=intrtdc_colidxacc, criteria1:=rngcurrrow.cells(1, intdtsource_colidxacccode), operator:=xland rngrtdc_alldetail.autofilter field:=intrtdc_colidxtext, criteria1:=rngcurrrow.cells(1, strcurracccodetext), operator:=xland dim rngresult range set rngresult = rngrtdc_alldetail.rows.specialcells(xlcelltypevisible)'rngrtdc_alldetail.specialcells(xlcelltypevisible) not work 'after filter, 1 result or none if (rngresult.rows.count > 0) 'if filter have result, here. else 'add new row end if next
my problem after filter, excelworksheet, can see have 1 record, rngresult.rows.count = 2 'for first filter record (that have 1 row only) in rngrtdc_alldetail, suspect due include header, not sure wrong.
rngresult.rows.count = 1 'for rest of filter record have 1 row
even worse when there no record after filter, rngresult.rows.count = 1
any advice appreciate. tq.
ok. after spent time on it, found out solution already. below note facing similar problem.
objective: insert "value" columnc, when columna = "a" , columnb ="b" , row between 1 10
b c 1 columna | columnb | columnc 2 | b | value 3 | x | 4 x | x | 5 x | x | 6 c | b | 7 | b | value 8 x | x | 9 x | x | 10 | b | value 11 | b | 12 | b | ... 'insert value @ columnc activesheet.range("a1:b10").autofilter field:=1, criteria1:="a", operator:=xland activesheet.range("a1:b10").autofilter field:=2, criteria1:="b", operator:=xland dim rng range each rng in activesheet.autofilter.range.range("a1:b10").specialcells(xlcelltypevisible).rows if (rng.row <> 1) 'no header activesheet.cells(rng.row, "c") = "value" 'set value @ c2,c7,c10 end if next rng 'count total row visible dim rnga range set rnga = activesheet.autofilter.range.range("a1:b10") debug.print rnga.columns(1).specialcells(xlcelltypevisible).count - 1 'result 3 'reference:http://www.contextures.com/xlautofilter03.html
note1**: "activesheet.autofilter.range" include header , below row visible row.
note2**: "activesheet.autofilter.range.offset(1, 0).specialcells(xlcelltypevisible).rows" offset range 1 row below only, not suitable if need set value @ result row.
excel vba excel-vba
Comments
Post a Comment