问题
A recurring Excel problem I have is formulas such as INDEX(array,row,column)
that return 0 when there's no result, rather than returning blank.
What is the best way to change the zero result to blank?
Here are approaches that I have tried so far:
1) Using division by zero. If INDEX returns 0, I cause an error that I then filter out.
=IFERROR(1/1/INDEX(A,B,C),"")
CONS: Makes the formula more messy and hides errors you may want to see.
2) Using custom formatting
0;-0;;@
CONS:
1) can't simultaneously apply date format
2) It doesn't work with conditional formatting when it comes to checking for blank cells (there is still the value of zero, it's just not shown)
3) UsingIF
statements
=IF((1/1/INDEX(A,B,C))<>"",(1/1/INDEX(A,B,C)),"")
CONS: Messy repetition
Does anyone have any other/better ideas?
回答1:
You can create your own user defined functions in a module within Excel such as (from memory, so may need some debugging, and the syntax may vary among Excel versions as well):
Public Function ZeroToBlank (x As Integer) As String
If x = 0 then
ZeroToBlank = ""
Else
ZeroToBlank = CStr(x)
End If
End Function
You can then simply insert =ZeroToBlank (Index (a,b,c))
into your cell.
There's a nice tutorial on just this subject here.
The basic steps are:
- Open the VB editor within Excel by using
Tools -> Macro -> Visual Basic Editor
. - Create a new module with
Insert -> Module
. - Enter the above function into that module.
- In the cells where you want to call that function, enter the formula
=ZeroToBlank (<<whatever>>)
where<<whatever>>
is the value you wish to use blank for if it's zero. - Note that this function returns a string so, if you want it to look like a number, you may want to right justify the cells.
Note that there may be minor variations depending on which version of Excel you have. My version of Excel is 2002 which admittedly is pretty old, but it still does everything I need of it.
回答2:
The normal way would be the IF statement, though simpler than your example:
=IF(INDEX(a,b,c),INDEX(a,b,c),"")
No need to do gyrations with the formula, since zero values trigger the false condition.
回答3:
I am not sure it works with all type of data but the only solution I found for the moment is to test if index returns blank:
=IF(ISBLANK(INDEX(a,b,c)),"",INDEX(a,b,c))
The formula
=IF(INDEX(a,b,c),INDEX(a,b,c),"")
does not work with text
回答4:
Perhaps the easiest way is to add the text formatting condition to the formula, with a ?
modifier. Thus:
(formula to grab values)
becomes:
text((formula to grab values),"?")
Hope that helps.
回答5:
If you’re willing to cause all zeroes in the worksheet to disappear, go into “Excel Options”, “Advanced” page, “Display options for this worksheet” section, and clear the “Show a zero in cells that have a zero value” checkbox. (This is the navigation for Excel 2007; YMMV.)
Regarding your answer (2), you can save a couple of keystrokes by typing 0;-0;
–– as far as I can tell, that’s equivalent to 0;-0;;@
. Conversely, if you want to be a little more general, you can use the format General;-General;
. No, that doesn’t automagically handle dates, but, as Barry points out, if you’re expecting a date value, you can use a format like d-mmm-yyyy;;
.
回答6:
The question may be why would you want it to act different from how it does right now? Apart from writing your own enveloping function or an alternative function in VBA (which will probably cause calculation speed reduction in large files) there might not be a single solution to your different problems.
Any follow up formula's would most probably fail over a blank thus cause an error that you would capture with IFERROR()
or prevent by IF(sourcecell<>"";...)
, if you would use the latter then testing for a zero is just the same amount of work and clutter. Checking for blank cells becomes checking for 0 valued cells. (if this doenst work for you please explain more specific what the problem is).
For esthetic purposes the custom formatting solution would be just fine.
For charts there might be an issue, which would be solved by applying it in the original formula indeed.
回答7:
There is a very simple answer to this messy problem--the SUBSTITUTE function. In your example above:
=IF((1/1/INDEX(A,B,C))<>"",(1/1/INDEX(A,B,C)),"")
Can be rewritten as follows:
=SUBSTITUTE((1/1/INDEX(A,B,C), " ", "")
回答8:
I figured it out, by concatenating an EMPTY
string.
INDEX(tt_Attributes,MATCH([RowID],tt_Attributes[RowID],0),COLUMN(tt_Attributes[Long Description]) ) & ""
回答9:
=if(b2 = "", "", b2)
This worked for me
回答10:
Use conditional formatting (Home tab, styles section) and apply highlight cells rule (putting 0 in the Format cells that are equal to box) but select custom format then the Number tab. Select Category custom and in the type box put:
0;-0;;@
Sounds complicated but is actually simple.
This gives the advantage that the cell looks empty but 0 is still the underlying value, so any formulas you use against that cell/selection will still see it as being numeric and saves on lots of messing around with chained IF statements.
回答11:
None of the above worked for me today, so I tried putting the 0 in quotes, as shown in the example below.
Example: =IF(INDEX(a,b,c)="0","", INDEX(a,b,c))
回答12:
Just append an empty string to the end. It forces Excel to see it for what it is.
For example:
=IFERROR(1/1/INDEX(A,B,C) & "","")
回答13:
=IF(INDEX(a,b,c)="0","", INDEX(a,b,c))
worked for me with a minor modification. Excluding the 0 and no spaces in between quotations:
=IF(INDEX(a,b,c)="","", INDEX(a,b,c))
来源:https://stackoverflow.com/questions/15239466/have-excel-formulas-that-return-0-make-the-result-blank