excel - How to tell macro to perform Match, Index formula using another cell with Match, Index data? -
i have excel workbook 2 master sheets , 1 data entry sheet.
project master:
|project no |asset no | |------------|------------| |p01 |a01 |
asset master:
|asset no |description | |-----------|--------------| |a01 |testing |
for data entry sheet, want use project no reference , use index/match formula in vba search other 2 fields. in case:
|project no |asset no |description | |------------|-----------|-------------| |p01 |a01 |testing |
i want data entry sheet change selected row instead of refreshing entire sheet whenever change single cell. in vba data entry sheet used code:
private sub worksheet_change(byval target range) if not intersect(target, range("a9:a9999")) nothing '--------------------------------------------------------------- target.offset(0, 1) .formular1c1 = "=if(isna(index(projectentry,match(rc1,projectentry[project no],false),2)),"""",index(projectentry,match(rc1,projectentry[project no],false),2))" .value = .value end target.offset(0, 2) .formular1c1 = "=if(isna(index(assetmaster,match(rc1,assetmaster[asset no],false),2)),"""",index(assetmaster,match(rc1,assetmaster[asset no],false),2))" .value = .value end end if end sub
when used code, asset no appears while description remains empty. code should supposedly perform change in row whenever cell in range selected (a9:a9999) has value changed.
is due code limitation having refer 2 master sheets refers project master while asset master ignored? there way solve issue?
you have bug in second formula. should read:
target.offset(0, 2) .formular1c1 = "=if(isna(index(assetmaster,match(rc2,assetmaster[asset no],false),2)),"""",index(assetmaster,match(rc2,assetmaster[asset no],false),2))" .value = .value end
the rc1
have should rc2
(or rc[-1]
).
edit:
a better formula use (thanks excelinefendisi) can seen in following code:
private sub worksheet_change(byval target range) if intersect(target, range("a9:a9999")) nothing exit sub '--------------------------------------------------------------- target.offset(0, 1) .formular1c1 = "=iferror(index(projectentry[asset no],match(rc[-1],projectentry[project no],0)),"""")" .value = .value end target.offset(0, 2) .formular1c1 = "=iferror(index(assetmaster[description],match(rc[-1],assetmaster[asset no],0)),"""")" .value = .value end end sub
but, lot of comments suggest, best way refresh edited row calculations in vba , write results sheet.
the following code using tables' listobject
objects:
private sub worksheet_change(byval target range) if intersect(target, range("a9:a9999")) nothing exit sub '--------------------------------------------------------------- dim Ä excel.application: set Ä = excel.application dim varvalue variant varvalue = Ä.index(Ä.range("projectentry[asset no]"), Ä.match(target.value2, Ä.range("projectentry[project no]"), 0)) target.offset(0, 1).value = iif(iserror(varvalue), vbnullstring, varvalue) varvalue = Ä.index(Ä.range("assetmaster[description]"), Ä.match(varvalue, Ä.range("assetmaster[asset no]"), 0)) target.offset(0, 2).value = iif(iserror(varvalue), vbnullstring, varvalue) end sub
note usage of application.
instead of worksheetfunction.
access worksheet functions. this, coupled use of variant type variable, allows trap error occurs if match fails.
Comments
Post a Comment