简体   繁体   中英

Assign an Excel function result to a variable within a cell

I'm working on an Excel spreadsheet where I'm using VLOOKUP to get data from another sheet. I have it working, but it's giving me an annoying "0" for blank results. I'd like to incorporate ISBLANK to address it.

My problem is the syntax. I REALLY do NOT want to retype the entire VLOOKUP function for the results if it returns FALSE.

In other words, what I'm trying to avoid doing is this:

=IF(ISNULL(VLOOKUP($A2,Sheet!$B$1:Sheet!$C$200,2)),"",VLOOKUP($A2,Sheet!$B$1:Sheet!$C$200,2))

Ideally, what I want to do is something like this:

=IF(ISNULL(VLOOKUP($A2,Sheet!$B$1:Sheet!$C$200,2)),"",[some variable that stores the result of the VLOOKUP])

Ideas, anyone? Thanks...

Are you open to formatting? You can hide the zeros that way, and therefore keep the formula short.

Highlight the range of the VLOOKUP() results, and go to Formatting, then Custom. Use this format and any 0 will be effectively hidden:

0;-0;;@

在此处输入图片说明

It depends whether you're looking up text or numbers.

If you want the result of your expression to be text that you're looking up, it's as simple as wrapping your VLOOKUP with the function T() .

So, for text , replace VLOOKUP($A2,Sheet!$B$1:Sheet!$C$200,2,FALSE) with T(VLOOKUP($A2,Sheet!$B$1:Sheet!$C$200,2,FALSE))

If the result of your expression is a number, and your VLOOKUP expression is too complicated to repeat, then you can avoid writing it twice by sneakily using IFERROR . The trick is to make it so that when VLOOKUP returns 0 , it makes an error, but otherwise it returns what VLOOKUP returned. And you can do this by taking advantage of some maths: Where x<>0 , then 1/(1/x) = x . When x=0 , Excel returns #DIV/0! . If we use IFERROR , then we can replace that with an empty string ( "" )

So, for numbers , replace VLOOKUP($A2,Sheet!$B$1:Sheet!$C$200,2,FALSE) with IFERROR(1/(1/VLOOKUP($A2,Sheet!$B$1:Sheet!$C$200,2,FALSE)),"")

By the looks of it the formula you're asking for is:

=IF(VLOOKUP($A2,Sheet!$B$1:$C$200,2,FALSE)="","",VLOOKUP($A2,Sheet!$B$1:$C$200,2,FALSE))  

Edit: The FALSE at the end of the function is so it only finds an exact match. Omitting it, or setting it as True , returns an exact or the next largest value that is less than the lookup value - your list must be sorted in ascending order for that to work.

Note the range reference to the sheet called Sheet is Sheet!$B$1:$C$200 and not Sheet!$B$1:Sheet!$C$200 .

Having said that , an easier way is just to hide the 0 returns with number formatting.
Give the cell a custom number format of:

#,##0;-#,##0;;@

The first section #,##0 is the format for positive numbers, the second -#,##0 for negative numbers, the third ;; (no format set) for zeros and the last @ for text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM