Close this search box.

Check if a range has all numbers from 1 to n [Homework]



Finally, spring weather showed up in Wellington this week. We cashed it as much as possible by going on treks, cycling trips, more treks and of course doing laundry. Here is a picture of the little ones, from today’s walk at Percy scenic reserve.


Anyways, I don’t have time to blog. I must go out and help kids with some cycling. But I want to keep you busy this weekend. So here is a fun homework problem.

Does my range have all numbers from 1 to n?

Let’s say you have a range called range (duh!).  And you want to check if range has all the numbers 1 to n (say n=5) in it, each number appearing only once (no more, no less). You can assume the named ranges range and in your formulas.

See below examples to understand the problem.


So go ahead and post your formulas in the comments section. I will sneak in whenever I can to look at all your creative answers.

Want more homework?

Whether you are down with bad weather or just want your fix of speadsheets for the weekend, we got you covered. Check out below puzzling problems. Be warned though, the rabbit hole is deep and mesmerizing.


Share this tip with your colleagues

Excel and Power BI tips - Chandoo.org Newsletter

Get FREE Excel + Power BI Tips

Simple, fun and useful emails, once per week.

Learn & be awesome.

Welcome to Chandoo.org

Thank you so much for visiting. My aim is to make you awesome in Excel & Power BI. I do this by sharing videos, tips, examples and downloads on this website. There are more than 1,000 pages with all things Excel, Power BI, Dashboards & VBA here. Go ahead and spend few minutes to be AWESOME.

Read my storyFREE Excel tips book

Excel School made me great at work.

– Brenda

Excel formula list - 100+ examples and howto guide for you

From simple to complex, there is a formula for every occasion. Check out the list now.

Calendars, invoices, trackers and much more. All free, fun and fantastic.

Advanced Pivot Table tricks

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It's all here.

Still on fence about Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

47 Responses to “Check if a range has all numbers from 1 to n [Homework]”

  1. Jason says:


    Are all the following true:
    Are there as many entries in 'range' as 'n'?
    Is the smallest number 1?
    Is the largest number 'n'?
    Is the product of all the numbers in 'range' the same as n factorial?


    • NARAYAN says:

      Hi Jason ,

      That last test :

      Is the product of all the numbers in 'range' the same as n factorial?

      is not necessarily a valid test to check whether the range includes every number from 1 to n , and only those numbers. Always be wary of using products , since the same product can be obtained through different factors .

      For example if n = 10 , FACT(10) = 3,628,800

      This same value can be obtained by the following 2 sets of numbers :

      1,2,3,4,5,6,7,8,9,10 ------- this is a valid set in this problem

      1,2,3,2,10,6,7,8,9,10 ------ this is an invalid set in this problem.

      Unfortunately , the other checks for smallest number , largest number , and number of entries in the range will all be satisfied by this invalid set.

      • Jason says:

        Aha! Thank you for pointing this out. I was trying to keep as 'simple' and 'logical' as I could, but it would appear I need a duplicate check in there too? My worry would be that some of the more advanced formulae would be too much for someone that would inherit a spread sheet containing such entries.

  2. Chirayu says:


  3. EXCELent Bacon says:

    Create a range with the complete reference series in it. For this example, let's call it RefRange.

    Array function:


  4. Micah Dail says:

    Array entered.


  5. GMF says:

    If you want to know which number(s) from the range are missing, copy this CSE formula down.

    • U4 says:

      Bravo! Thanks excel gurus. But suppose the has names and not numbers; and we want to find out the missing name/ word? Let us try to figure that out.

      • GMF says:

        RefRange has the list of names you want to check (e.g. list of invitees to a meeting) and SourceRange has the list of people who actually attended. CSE formula copied down.


        • Ufoo says:

          Thanks but this formula is returning the first name in the list. Not the name which is missing. Am I making some mistake??

          • GMF says:

            Hard to tell without looking at the data. The ROW(SourceRange) should return the first row of the SourceRange, so try replacing that with a hardcoded address like ROW($A$2) or wherever your source range starts. And of course enter it as an array/CSE formula.

          • GMF says:

            Another option (a bit longer) that doesn't require hardcoding the start of the source range is

            {=INDEX(MissingSourceRange, SMALL(IF(COUNTIF(MissingRefRange, MissingSourceRange)=0, MATCH(ROW(MissingSourceRange), ROW(MissingSourceRange)), ""),ROW(1:1)))}

          • Ufoo says:

            This formula works perfectly. Thanks a lot. And you have taught me another way of generating serial numbers; MATCH(ROW($A$2:$A$9), ROW($A$2:$A$9)

  6. Leonid says:


  7. RadioMo says:

    Using Gauss' sum of an arithmetic series formula, for series starting with 1:

  8. Michael says:


    • Michael says:

      Whoops forgot to change my reference to N in the previous. I originally had N in $I$1, but changed it to a named formula.


  9. Gary Ferguson says:

    My forumula:
    entered as array

  10. Marcus Croucher says:

    Similar (but more complex) than Micheal's one above - his one is more Pareto optimal than mine!


  11. Denys Calvin says:


  12. Narendra Babu says:


  13. SABEESH says:


  14. GraH says:

    I really need to investigate array formulas quickly to upgrade my skill!
    Fantastic replies to analyse and learn from.
    Saying thank you a lot on this blog lately, but I am just very grateful...
    So keep on going ladies and gentlemen to share your Excellent knowledge.

  15. Irving says:

    At first I thought in SUMPRODUCT, but; to avoid repeating a solution from above, something less sofisticated :)...



  16. Jim says:


  17. Jim says:

    Sorry - needs to have before 5

  18. Marydas Joseph says:


  19. Simon says:

    The most elegant to me is:

    This checks that every number from 1 to n appears just once in the range. If you also want to check that those are the ONLY things in the list then an additional check is needed:
    =AND(COUNTIF(range,ROW(INDIRECT("1:"&n))), COUNTA(range)=n)

  20. Marydas Joseph says:

    Hai friends...

    I suggest two formulas here.First formula self detect the maximum number and with this we can avoid link with one external cell.All the "formula range" is inside the "Range"



  21. Steve James says:


    array entered

  22. Jason Morin says:

    J1 = n
    Handles duplicates


  23. B-art in XL says:


    Where F2 contains the value N

  24. CC says:


  25. LAM says:

    =SUMPRODUCT(--(1<=list)*(list<=5)) = COUNTA(list)
    while the following can be replaced: 1 for min and 5 for max, this answers TRUE or FALSE to the task at hand

    • LAM says:

      if we wish to check only the number entries:
      =SUMPRODUCT(--(1<=list)*(list<=5)*ISNUMBER(list)) = SUMPRODUCT(--ISNUMBER(list))

  26. Vi Vi says:


  27. Rahul Mangla says:

    Range is a1:a10 where we enter numbers
    Enter First Number in Cell b12
    Enter Second Number in cell b13
    Enter Range: Cell B14

    Here is formula

    Simple maths...SUM, PRODUCT, AVERAGE of a simple number series will be unique...would never match if numbers in the series are manipulated.

  28. Jina says:

    Need help!
    How do you add column B. If B1 to B365 is equal to 1. If there is zero do not add and start again to the first row that is equal to 1. I need to know if there is straight 30, 60 and 90 days. Column A is the date field and Column B contains either 1 (without late) or 0 (with late).
    Ho many 30 days in a year? How many 60 days and how many 90 days without late in a year?

    I will appreciate any help.

Leave a Reply