(VBA) Excel : action if content in two columns is matching -
a picture worth thousand words make clear, here picture illustrate situation:
so have in column c contents (string , integer) c2 c16. have column g city names (string) g21 g25.
here try : want method probacity() matches between these 2 columns. if there match, want display value (10 example) in corresponding row of g column (in array "probablity of being city"), if no match, it's gonna 0.
so, here in cell g13 should write 10 there match "quezon" , 0 in other cells of array.
here code :
sub probacity() dim count integer, myresult boolean count = 2 lastrow range("c" & count).activate myresult = isnumeric(application.match(activecell.value, range("g21:g25"), 0)) if myresult = true range("g" & activecell.row).value = range("g" & activecell.row).value + 0 else range("g" & activecell.row).value = range("g" & activecell.row).value + 10 end if next count end sub
the problem code myresult returned value false (so set 0 in array).
thanks lot help.
edit: after trying suggestion :
1/ using vba (working "quezon") 2/ using formulas... same issue.
i want method probacity() matches between these 2 columns. if there match, want display value (10 example) in corresponding row of g column (in array "probablity of being city"), if no match, it's gonna 0.
no need use vba this.
see example
use array formula. have press ctrl+shift+enter after entering formula
=if(count(find($c$9:$c$11,a1)),"10","")
screenshot
edit: since want vba method, try this.
vba method (this work sample data per original post)
sub sample() dim ws worksheet dim lookuprange range dim lrow long '~~> change relevant sheet set ws = thisworkbook.sheets("sheet1") ws '~~> city list set lookuprange = .range("g21:g25") lrow = .range("c" & .rows.count).end(xlup).row = 2 lrow .range("g" & i).value = application.evaluate("=if(count(find(" & _ lookuprange.address & _ "," & .range("c" & i).address & ")),""10"",""0"")") next end end sub
Comments
Post a Comment