0

I have the following formula:

=IF(MAX(C2:F2)>0,COUNTIF(C2:F2,">1")/COUNT(C2:F2),0)

This formula is located in the second column of a structured table, and goes through the rest of the columns in the table to the right. The formula basically tells how many instances satisfy a certain criteria and puts it in a fraction over the total number of instances. So if the criteria is met 2 times and there were 3 instances I get 2/3 as the output.

I have been researching for an hour now and I can't figure it out. Is there a way to get the cell range in the formula to auto-expand when a new column is added?

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
Duck9139
  • 69
  • 10
  • @pnuts - And to the rest of you who voted to close this: My question has nothing to do with general hardware and software... Seriously??? I asked about adjusting a formula in a structured table... As far as I can tell from the Meta, this is allowed. – Duck9139 Aug 10 '18 at 12:28

2 Answers2

2

You should take advantage of Structured Referencing and Index to get the parts of the table you need

=IFERROR( COUNTIF(
                  INDEX(YourTableName[@],1,(COLUMN([@FormulaColumn])-COLUMN(YourTableName[@])+2)):
                  INDEX(YourTableName[@],1,COLUMNS(YourTableName[@])),
                  ">1") /
          COUNT(
                  INDEX(YourTableName[@],1,(COLUMN([@FormulaColumn])-COLUMN(YourTableName[@])+2)):
                  INDEX(YourTableName[@],1,COLUMNS(YourTableName[@])))
        ,0)

This will count number of cell to the right of the Formula Column that are >1 and divide that by the count of cells to the right of the Formula Column that are not blank.

The key point here is COLUMNS(YourTableName[@]). This will increase automatically as you add columns

Note: if you want to include blanks in the divisor count, the formula can be adjusted to suit

Just change YourTableName and FormulaColumn to suit your data

chris neilsen
  • 52,446
  • 10
  • 84
  • 123
  • Thank you! this answer serves my needs for this sheet perfectly! And thank you for sticking up for me above. I contacted support to see what they have to say about the matter. – Duck9139 Aug 10 '18 at 12:52
2

From what I can make of the data, the problem can be solved by creating a named range that refers to:

=OFFSET($C$2,0,0,1, COLUMNS(myTable))

The change to the formula is:

=IF(MAX(myRange)>0,COUNTIF(myRange,">1")/COUNT(myRANGE),0)
  • Where myTABLE is the name of your table and
  • Where myRANGE is the name of the new named range

How it works:

The named range counts the number of columns in the table and updates whenever one is added it removed. Offset anchors to the left side of the table and extends to the right by the number of columns in the table. The formula drops the cell reference in favor of the named range for improved legibility. It can be copied dragged pasted and moved all over the place and still point to the correct range. With adequate scope, you could put it anywhere in the workbook without issue.

Note: My biggest pet peeve about this method comes when copying the sheet to a a new workbook. Named ranges are so awesome that a workbook scoped range will point to the original workbook even after it has been copied to a new workbook. Sometimes that's a good thing, but us mostly it's a nuisance.


I believe an alternative answer will compliment the first answer because there are innumerable occasions where one may want a dynamic range and simultaneously not want a table.

And that's the clue, for more information you can search for dynamic range. Or more specifically: dynamic named range.


To answer your question:

Yes, it is possible and it's also very handy. For example, the print area can expand and contract with your data and it's very easy to interact with because you work with by name rather than address. And when using VBA, I find it's generally easier to work there named range than a table, that is, unless the table is bound to a variable, in that case they would be equal in my eye. There is just something a fundamentally loathe about typing listobject("table name").listcolum("table colum name").datapropertyrange I almost have a visceral reaction when I have to type it.

The gist of it creating a dynamic named range is simple. Open the name editor, name a range, and use offset() and counta() to define your range. Counting non-empty cells pulls double duty. it is the trigger to recalculate the range and it provides an index that can be used with offset to change the range area.

Here is one example: =OFFSET($A$1,0,0,COUNTA($A:$A),1)


It is very straight forward. With one exception, print range.

For whatever reason, print range doesn't behave the same way and doesn't update like a normal named range. To make it dynamic you create a named range like you normally would. This will be your print range so make it count. Then make your print range refer to that named range. That's all there is to it. The first named range points to the area you want to print and the print at range points to that named range.

girlvsdata
  • 1,596
  • 11
  • 21
ProfoundlyOblivious
  • 1,455
  • 1
  • 6
  • 12
  • All very interesting, but it doesn't answer _this_ question. Can you show the OP how to apply that to _their_ situation? – chris neilsen Aug 10 '18 at 02:26
  • Certainly. I provided a generic method that could be applied with our without a table to augment your answer. Now that I'll be answering the question directly you may consider this a challenge to your answer ;) – ProfoundlyOblivious Aug 10 '18 at 03:43
  • @ProfoundlyOblivious - Thank you for your answer! I tried yours out to see if I could get it to work and it did. I went with the other for this specific sheet, but I'm definitely keeping this in my back pocket for the future! – Duck9139 Aug 10 '18 at 12:54
  • Thanks for letting me know you used it. I get warm fuzzies when I make an effort to share knowledge and learn that is was applied, even if only in a test. – ProfoundlyOblivious Aug 10 '18 at 21:27