Skip to main content

Kutools for Office — One Suite. Five Tools. Get More Done.

How to create a number sequence based on criteria in Excel?

Author Xiaoyang Last modified

When working with Excel, you may often need to create a number sequence for a list of cells, such as assigning order numbers or serial IDs. For instance, a simple sequential list can easily be generated by dragging the fill handle. However, things get more challenging when your goal is to generate a sequence that depends on specific criteria—such as numbering items within groups, creating a running count of items matching a condition, or skipping certain rows based on cell values. Getting this right is important for tasks like data analysis, reporting, or preparing datasets for further processing, where conditional numbering can save time and help avoid manual errors.

This guide will explain practical solutions for generating sequences in Excel based on rules you set.

Create a number sequence based on duplicate entries with the COUNTIF function

Create a number sequence based on criteria with COUNTIF and IF functions


Create a number sequence based on duplicate entries with the COUNTIF function

Let's say you need to assign sequence numbers according to a group—such as creating a running number for each country in a list. For example, if you have several repeat entries for a country and want each to be numbered sequentially inside its own group, as illustrated in the screenshot below:

Create number sequence based on duplicate entries

To automatically assign serial numbers in this grouped way, you can use the COUNTIF function. This method is suitable for small to medium datasets where the group to be sequenced appears multiple times, and you want the count to restart for each group value. The main advantage is its simplicity—no additional tools or setup are needed. However, for large datasets, formula recalculation in each row may impact performance.

Here's how you can use COUNTIF to assign sequence numbers based on another column's group values:

1. Enter the following formula in the first cell of the sequence number column that corresponds to your data (e.g., if your countries are in column B starting from row 2, enter this in C2 or your designated cell):

=COUNTIF(B$2:B2,B2)

This formula counts the number of times each group value (e.g., country) has appeared from the first data row down to the current row, giving you a running total for each group. Remember to adjust the cell references if your data starts on a different row or is in another column.

2. After entering the formula, drag the fill handle down to apply this to the remaining cells in your sequence column. Each value will increment within its group, giving a clear, easy-to-read index for each repeated entry as shown below:

Create number sequence based on duplicate entries with COUNTIF function

If you see unexpected blanks or the sequence restarts incorrectly, double-check both your formula range anchoring (e.g., B$2:B2) and that there are no unwanted blank cells or merged rows in your data. Grouped numbering should adapt cleanly as long as the formula accurately refers to the data you wish to count.

a screenshot of kutools for excel ai

Unlock Excel Magic with Kutools AI

  • Smart Execution: Perform cell operations, analyze data, and create charts—all driven by simple commands.
  • Custom Formulas: Generate tailored formulas to streamline your workflows.
  • VBA Coding: Write and implement VBA code effortlessly.
  • Formula Interpretation: Understand complex formulas with ease.
  • Text Translation: Break language barriers within your spreadsheets.
Enhance your Excel capabilities with AI-powered tools. Download Now and experience efficiency like never before!

Create a number sequence based on criteria with COUNTIF and IF functions

You may encounter situations where you want to generate sequence numbers only for rows that match a specific criterion—for example, numbering only items that do not have "Total" in the description, or numbering where a certain column matches a given value. As a practical example, if column A includes product entries plus summary rows labeled "Total", and you wish to number only the actual data rows, skipping "Total" entries, you can accomplish this with an IF and COUNTIF formula.

Create number sequence based on criteria

Here's how you can set up such conditional numbering:

1. Type 1 (or your desired starting value) in the first cell of your sequence number column (e.g., C2), aligned with the first qualifying row in your data. For example:

 Enter the number1 into the first cell

2. In the next cell down (e.g., C3), enter the appropriate formula for your Excel version. These formulas ensure a sequence is generated only for rows that do not match the "Total" criteria:

=IF(@$A$2:$A$14="Total","",COUNTIF($C$2:C2,">0")+1)        (Excel365)
=IF($A$2:$A$14="Total","",COUNTIF($C$2:C2,">0")+1)           (Other Excel versions)

Note: In these formulas, $A$2:$A$14="Total" defines the range containing the criteria to check; C2 is the cell with the initial sequence number. Update these references based on your actual data positions. Mismatched or misaligned ranges can cause all results to be blank or counts to skip rows unexpectedly.

3. Drag the fill handle down to apply the formula to the rest of the sequence column—extending as far as needed (e.g., down to C14). You’ll see that only rows not containing your criteria receive incrementing numbers; the "Total" rows remain blank as desired:

Create number sequence based on criteria with COUNTIF and IF functions

This approach works well for short to mid-sized tables, letting you quickly sequence rows based on virtually any text, number, or logical condition. If your data structure changes regularly, remember to update cell references to cover your full data range.

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more
Use Kutools in your preferred language – supports English, Spanish, German, French, Chinese, and 40+ others!

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!

All Kutools add-ins. One installer

Kutools for Office suite bundles add-ins for Excel, Word, Outlook & PowerPoint plus Office Tab Pro, which is ideal for teams working across Office apps.

Excel Word Outlook Tabs PowerPoint
  • All-in-one suite — Excel, Word, Outlook & PowerPoint add-ins + Office Tab Pro
  • One installer, one license — set up in minutes (MSI-ready)
  • Works better together — streamlined productivity across Office apps
  • 30-day full-featured trial — no registration, no credit card
  • Best value — save vs buying individual add-in