0

Apologies for the title, unsure of how to ask this question. I am trying to specify the range of data I have in a column on excel, where the range of relevant values change. So to visualize, data could be in cells A1-A4, with zeros below (or data in cells A1-A3, or A1-A25, etc.). By using countif, I can get the number of non-zero values in the changing range, which is useful because it specifies the ending row I want for my range. My issue is how I use this value in a formula. In my head (if I put the countif formula in B1), the range would look something like this: $A$1:$A$(the value from B1) but I am not sure how to put this into practice. Any ideas?

2
  • So there is actual data below the range of interest in the form of zeros? Or is rest of the column empty? Maybe some sample data and desired outcome would be helpful to visualise
    – JvdV
    May 28, 2019 at 15:15
  • Yes, actual data. The data is based on an if statement, something like =IF(x=T, Y, "")
    – EDS
    May 29, 2019 at 17:13

2 Answers 2

3

Indirect and Offset are volatile, and while one volatile formula will not cause an issue many will slow down the calcs. Volatile functions re-calc every time Excel recalcs, whether the data changed or not.

I prefer INDEX to them:

$A$1:INDEX(A:A,COUNTIF(A:A,">0"))

But even COUNTIF() if used too many times can slow down the calcs. You can use MATCH in this instance:

$A$1:INDEX(A:A,MATCH(0,A:A,0)-1)

Which will now find the first 0 and set the cell above that as the last in the range.

3

You can use the INDIRECT or OFFSET functions

Using INDIRECT is the simpler approach: to specify the range starting in Column A, Row 1 and ending in Column A, Row COUNTIF(A:A,">0"), you would just write INDIRECT("A1:A" & COUNTIF(A:A, ">0")) - so, if there are 7 cells with values greater than 0, this becomes INDIRECT("A1:A7")

Using OFFSET is more complicated - you have to specify how many cells left/down to move (0 and 0), how many rows of data to get (COUNTIF(A:A, ">0")), and how many columns of data to get (1), from your Starting position (A1) - so it looks something like this instead: OFFSET(A1, 0, 0, COUNTIF(A:A, ">0"), 1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.