Numerar — Celdas En Excel Con Condiciones
that also ignores blanks:
The principle is sound: you must create a helper column that marks visibility ( =SUBTOTAL(103, A2) ), then use COUNTIFS on that helper column. This pushes Excel to its logical limits. To number cells with conditions is to understand that spreadsheets are not merely ledgers but interactive models. The simple fill handle sees no difference between a data row and an empty spacer. The conditional formula, however, sees context: blanks, filters, categories.
This requires COUNTIFS (or SUMIFS with a logical trick). Assume Column A is Category, Column B is Item. In C2:
This is where becomes essential. It transforms Excel from a static grid into a dynamic database engine. Conditional numbering is not about counting cells; it is about assigning an incremental identity based on logical tests. This essay explores the three primary paradigms for conditional numbering in Excel: the COUNTIF expanding range, the SUBTOTAL function for filtered data, and the COUNTIFS multi-condition ranking. 1. The Classic Sequential Condition: The Expanding Range The most fundamental conditional numbering problem is: "Number only the rows where Column A is not empty, ignoring blanks." numerar celdas en excel con condiciones
This mimics the behavior of a for loop in programming without VBA. The formula carries its own history. It is stateful —each cell’s output depends on the count of previous cells. This is the foundation of running totals and ranked lists. However, it fails catastrophically with filters or hidden rows, because COUNTA sees hidden cells. 2. The Invisible Condition: Numbering Filtered Data When you apply a filter to a table, rows become hidden. A standard COUNTA formula will break the sequence, creating gaps (e.g., 1, 2, 5, 7). The user needs a numbering system that sees only the visible universe.
Using LET (Excel 365):
=IF(ISBLANK(A2),"",COUNTA(A$2:A2))
The range A$2:A2 is the key. As the formula is copied down, the top anchor remains fixed (A$2), while the bottom expands (A2 becomes A3, A4, etc.). The COUNTA function counts only non-blank cells in this expanding window. Because the IF statement checks the current row first, only rows with data receive a number. The blanks receive an empty string, preserving the visual hierarchy.
This is a form of window function (similar to ROW_NUMBER() OVER (PARTITION BY Category) in SQL). It demonstrates that Excel’s grid can perform relational database operations without a database engine. This technique is invaluable for creating outlines, bill of materials (BOM) exploded views, or numbered lists inside pivot table source data. 4. The Advanced Synthesis: Combining Visibility and Hierarchy The ultimate challenge: number visible rows only, restarting the count per group, after a filter. This requires an array formula (or the new LET and FILTER functions in modern Excel).
SUBTOTAL(103, A2) checks if the current row is visible (returning 1 if visible, 0 if hidden or filtered). If visible, the second SUBTOTAL(103, A$2:A2) counts the number of visible cells in the expanding range. This creates a sequential, gapless index that updates instantly when you change the filter. that also ignores blanks: The principle is sound:
=IF(A2="", "", COUNTIFS(A$2:A2, A2, B$2:B2, "<>"))
Mastering COUNTA , SUBTOTAL , and COUNTIFS for numbering teaches a deeper lesson: Each cell is a pure function (or should be) of the cells above it. Conditional numbering forces the user to think in terms of state , scope , and visibility —concepts usually reserved for software engineering.
Thus, the next time you need to number a list, do not drag the fill handle. Ask: What is the condition? If the answer is “just count everything,” use the fill handle. But if the answer involves “except,” “only if,” “per group,” or “when visible,” you have entered the realm of conditional numbering—where formulas become algorithms, and rows become records. The simple fill handle sees no difference between
The solution lies in a counter-intuitive use of COUNTIF or COUNTA with a mixed reference. In cell B2, you enter:
=LET( visible, SUBTOTAL(103, A2), group, A2, IF(visible, COUNTIFS(A$2:A2, group, SUBTOTAL(103, OFFSET(A$2, ROW(A$2:A2)-ROW(A$2), 0)), 1), "") ) (This is a conceptual simplification; the actual implementation often requires helper columns for performance.)