vlookup if not in Table

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to lookup a Table to find values, but somtimes my entry is not present
and the function returns '#NA'. In this case I want a blank in column M,
otherwise some function.

The following code does not run and has an error somewhere in the
ActiveCell.Formula.
Thanks for any help.

Range("M2").Select
ActiveCell.Formula =
_"=IF((VLOOKUP(B2,'LookupTable'!$A$1:$B$100,1,FALSE)="#NA"),"",IF(G2>0,(G2-J2)*E2/G2,""))"
With ActiveSheet
.Range("M2").AutoFill Destination:=Range("M" & lastrow),
Type:=xlFillDefault
End With
 
Try doubling up on the double quotes and using =if(iserror(

ActiveCell.Formula = _
"=IF(iserror(VLOOKUP(B2,'LookupTable'!$A$1:$B$100,1,FALSE)),""""," _
& "IF(G2>0,(G2-J2)*E2/G2,""""))"

But since you're only checking column A, you could use =if(iserror(match(...)))

ActiveCell.Formula = _
"=IF(iserror(match(B2,'LookupTable'!$A$1:$a$100,0)),""""," _
& "IF(G2>0,(G2-J2)*E2/G2,""""))"
 
While I would be more inclined to use a countif to see if your value exists
you can use vlookup. What you want to do is to the the ISNA function to check
the return value something like this...

ActiveCell.Formula =
_"=IF(ISNA(VLOOKUP(B2,'LookupTable'!$A$1:$B$100,1,FALSE)),"",IF(G2>0,(G2-J2)*E2/G2,""))"
 
Sorry as Dave points out you need the double quotes...

ActiveCell.Formula =
_"=IF(ISNA(VLOOKUP(B2,'LookupTable'!$A$1:$B$100,1,FALSE)),"""",IF(G2>0,(G2-J2)*E2/G2,""""))"

or

ActiveCell.Formula =
_"=IF(CountIf('LookupTable'!$A$1:$B$100,B2)=0,"""",IF(G2>0,(G2-J2)*E2/G2,""""))"
 
Thanks Dave and Jim

Dave Peterson said:
Try doubling up on the double quotes and using =if(iserror(

ActiveCell.Formula = _
"=IF(iserror(VLOOKUP(B2,'LookupTable'!$A$1:$B$100,1,FALSE)),""""," _
& "IF(G2>0,(G2-J2)*E2/G2,""""))"

But since you're only checking column A, you could use =if(iserror(match(...)))

ActiveCell.Formula = _
"=IF(iserror(match(B2,'LookupTable'!$A$1:$a$100,0)),""""," _
& "IF(G2>0,(G2-J2)*E2/G2,""""))"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top