问题
=IF(ROWS(AA$38:AA38)>COUNTIF(U$38:U$1000,"<>0"),"",INDEX(U$38:U$1000,SMALL(IF(U$38:U$1000>0,ROW(U$38:U$1000)-ROW(U$38)+1),ROWS(AA$38:AA38))))
I'm trying to use this formula to find and return all the non-zero values in a column of data (starting from row 38), but I don't know how many rows there will be before I import the data. I'd like to be able to just automate the sorting, but if I use (U:U
) or pick a much larger number than the actual number of filled rows (eg. U$38:U$20000
), I get an error.
Is there a way of either:
a) getting excel to automatically change the (U$38:U$####
) to the number of rows filled with data? (ie, if there are 400 rows, it would change to (U$38:U$400
).)
OR
b) finding an alternative formula that can pick out all the non-zero values, without knowing the number of rows?
EDIT:
As you can see from the image below, I've set up my spreadsheet so that it not only returns the non-zero values (from column U), but also the time (from column AG) that it corresponds to. The problem is when I try to increase the range.
I'll be looking at lots of different data files with varying amounts of data, so I would like to be able to automate it - and not have to fiddle about with the all the U$38:U$1000
's every time I need to change the range. Does that make sense?
Is it possible with VBA instead?
Example Data
回答1:
Try this in some column:
=IFERROR(INDEX($U:$U,SMALL(ROW(myRange)*(myRange<>0),SUMPRODUCT(N(myRange=0))+ROWS($1:1))),"")
This is an array formula and must be confirmed by holding down ctrl + shift while hitting enter
Enter it in some cell. See that it returns the desired value, then fill down until it returns blanks.
myRange
is some arbitrary range larger than your largest row size. I used u38:u20000
EDIT: To return values from a matching row in a different column than U
, merely change the array
argument in the INDEX
function from $U:$U
to the desired column, eg: $AG:$AG
EDIT2: If you need to ignore error values in column U
, try this CSE entered formula:
=IFERROR(INDEX(U:U,SMALL(ROW(myRange)*(IFERROR(--myRange,0)<>0),SUMPRODUCT(N(IFERROR(--myRange,0)=0))+ROWS($1:1))),"")
回答2:
This a just a fix to the existing formula to make it work over a wider range, allow negative values and ignore empty cells, but (as I suspected) others may suggest more efficient solutions!
=IF(ROWS(AA$38:AA38)>COUNTIFS(U$38:U$10000,"<>0",U$38:U$10000,"<>"),"",INDEX(U$38:U$10000,SMALL(IF(U$38:U$10000<>0,ROW(U$38:U$10000)-ROW(U$38)+1),ROWS(AA$38:AA38))))
This can be made more efficient by declaring a dynamic range using Name Manager something like this:-
=Sheet1!$U$38:INDEX(Sheet1!$U$38:$U$10000,COUNT(Sheet1!$U$38:$U$10000))
and calling it (say) DynamicRange.
Then the formula becomes:-
=IFERROR(INDEX(DynamicRange,SMALL(IF(DynamicRange<>0,ROW(DynamicRange )-ROW(INDEX( DynamicRange,1 ))+1 ),ROWS($1:1))), "")
These are array formulae and must be entered using CtrlShiftEnter
来源:https://stackoverflow.com/questions/39657246/how-to-find-return-all-non-zero-values-within-an-unknown-range