Ravindra BagaleCourses & study guides मराठी Track your progress

9. Dynamic Arrays

Excel

9.14 WRAPROWS and WRAPCOLS: Turn One Long List into a Grid

Rebuild a grid from a list with a known grouping rule

Amir copies a short fictional order summary from the workshop notes for Blinkit and Amazon Now. Excel receives a single column. The first entry is an order ID, then a city, then an amount. The next order has the same three fields, again one below another. He could move each small group himself, but the worksheet can wrap the sequence into records with one formula.

A vector is a one-dimensional list, either one row or one column. To wrap is to take several consecutive values from that list and start a new row or column when the specified limit is reached. WRAPROWS fills across each result row. WRAPCOLS fills down each result column. The record order in the source determines which direction makes sense.

Predict the output shape and the incomplete group

Try five fictional inventory labels on a scratch sheet: Pen, Book, Bag, File and Box entered in A2:A6. In a blank rectangle use the two expressions below separately. The second argument 3 means either three values across one row or three values down one column.

=WRAPROWS(A2:A6,3,"-")
=WRAPCOLS(A2:A6,3,"-")
Both results preserve the same five inputs, with one extra display position.
FunctionShapeRead the grid by rows
WRAPROWS2 × 3Pen, Book, Bag / File, Box, dash
WRAPCOLS3 × 2Pen, File / Book, Box / Bag, dash
In the same five-item list, File goes to row 2 column 1 when wrapped across rows and row 1 column 2 when wrapped down columns. The dash goes in the remaining location.
Choose the direction that corresponds to what consecutive values represent.

To predict the shape when the limit is smaller than the length, divide the list length by the wrap count and round up to see how many groups are needed. Five values with a count of three need two groups. They supply five cells of a six-cell rectangle. The remaining location is padding, an output slot that has no corresponding input value. Padding has its own choice, and the default is #N/A.

If the wrap count is equal to or larger than the number of values, the documented result is simply one row for WRAPROWS or one column for WRAPCOLS. For instance, with the same five values and a count of seven, WRAPROWS gives a single row of five values. Use the EXPAND lesson when you require an output of exact size with additional marked positions.

Microsoft lists both wrapping functions for Excel 2024, Microsoft 365 and Excel for the web. Their input must be one row or one column. If you pass a rectangle with more than one row and more than one column, Excel documents a value error. Flatten an intentional grid first only after deciding the traversal order. A wrap below one is invalid. Use a positive whole count and verify the target shape with safe default data before putting the formula into a report.

Lab 1. Wrap three consecutive fields into each order row

On a new sheet named PastedOrders put Pasted value in A1. Enter the twelve values one below the other in A2:A13. Paste text without letting number-like IDs change type. Amount entries should be numeric. The order IDs used here contain letters and a hyphen, which makes their role visible.

PastedOrders A2:A13.
CellValueIntended field
A2BLK-1001Order ID
A3PuneCity
A464Amount
A5BLK-1002Order ID
A6NashikCity
A7270Amount
A8AMN-1003Order ID
A9PuneCity
A10110Amount
A11BLK-1004Order ID
A12NagpurCity
A13240Amount
  1. Put Order ID, City and Amount in C1:E1. Leave C2:E5 open. In C2 enter =WRAPROWS(A2:A13,3).
  2. Check C2:E2 for BLK-1001, Pune, 64. C3:E3 is BLK-1002, Nashik, 270. The formula starts a new record after taking each group of three.
  3. Check C5:E5 for BLK-1004, Nagpur, 240. Use =ROWS(C2#) and =COLUMNS(C2#) in free check cells. Expect four rows of three fields.
  4. In an independent cell use =SUM(CHOOSECOLS(C2#,3)) to get ₹684. Add the original amounts 64, 270, 110 and 240 directly to verify. Keep the expected total beside the exercise.
  5. For a practice copy, put AMN-1005 in A14 and change your input range to A2:A14. The fifth result row has the new ID and two default #N/A positions. It represents the beginning of an incomplete record.
The first three entries BLK-1001, Pune, 64 fill one record. The next three fill the next. With a thirteenth entry two fields in the final record need padding.
Trace the first field of each new row back to the expected source position.

See how consecutive values become a record

  1. Identify the first three fields

    Read BLK-1001, Pune and 64 in positions one, two and three.

  2. Wrap to another row

    The fourth value BLK-1002 restarts at the first field of the second row.

  3. Check the complete input

    Twelve input positions make exactly four complete records, total ₹684.

  4. Detect an incomplete tail

    A thirteenth ID starts another row, with two values still missing.

Preserve a missing-record warning before exporting

Setting a custom third argument such as "Not supplied" changes how the two missing positions display. A visually blank string still means padding, not a recorded empty City or zero Amount inside a complete source record. Use an explicit MOD test if the report must contain only complete groups. The number of source positions should divide evenly by three here.

=LET(sourceList,A2:A14,
     IF(MOD(ROWS(sourceList),3)<>0,"Incomplete order record",WRAPROWS(sourceList,3)))

With thirteen positions this returns the message. When testing the original twelve, replace the range with A2:A13 and verify it returns the four records. This guard checks cardinality, the number of values in the sequence. It cannot prove that each position contains the right type. If values were omitted in different groups, the final length might still be divisible by three after several omissions. Always check the field sequence and known identifiers as well.

Lab 2. Place each complete field group down a column

Another fictional export lists all four Order IDs first, all four cities next, and all four amounts last. Use a new tab named FieldSeries. Put Source value in A1 and enter this sequence in A2:A13. The three groups have the same length. Inside each group, the item positions represent the same orders in the same order.

BLK-1001
BLK-1002
AMN-1003
BLK-1004
Pune
Nashik
Pune
Nagpur
64
270
110
240
  1. In C1:E1 put Order ID, City and Amount. Each of these will be built from a consecutive four-item block. In C2 enter =WRAPCOLS(A2:A13,4).
  2. Read the first result column. It shows the four IDs in order because the function fills down four rows before moving to the next column.
  3. Read the first row across and verify BLK-1001, Pune, 64. Read the third and verify AMN-1003, Pune, 110. The fourth row is BLK-1004, Nagpur, 240.
  4. Reconcile the four numeric Amount rows to ₹684. It is the same record list as Lab 1, because the representation changed while every field's position still referred to the same order.
  5. Try =WRAPROWS(A2:A13,3) on this input in a free area. It also returns four rows of three, but the first row has three order IDs. The shape is mechanically reasonable and the records are wrong. Return to the documented order of incoming fields.

Check that equally long field groups describe the same records

In a temporary copy swap the second and third entries of the city 4-item group. The source city list becomes Pune, Pune, Nashik, Nagpur. The numeric totals and output size stay the same. BLK-1002 is now paired with Pune even though the source fixture says Nashik, and AMN-1003 gets the other city. The wrap can't infer which actual record a city belongs to. It reads matching positions among the groups.

If you can obtain IDs alongside each field as it arrives, use the keys to match the City and Amount values rather than assuming the source groups stayed in the same order. Positions are useful for an import with a documented order. For unordered data, reliable keys provide stronger evidence. Restore the proper order and verify both moved cities with their IDs.

Wrap a label sheet without inventing records

In another example the list contains simple labels rather than mixed fields. Use IDList, columns A and G as described below. Ten IDs can fill three labels across per row, leaving two empty display positions on the fourth row. Print position is a layout need rather than a different order definition. Keep the original ten-value list for counts or lookups.

On IDList place the ID in column A and Amount in G at the stated row. Row 1 contains the headers.
RowOrder IDAmount
2BLK-100164
3BLK-1002270
4AMN-1003110
5BLK-1004240
6AMN-1005232
7BLK-1006150
8BLK-10071299
9AMN-1008195
10BLK-1009220
11BLK-101090

Put =WRAPROWS(A2:A11,3,"-") in J2 with space for four rows and three columns. Its first row is BLK-1001, BLK-1002, AMN-1003. Its bottom row is BLK-1010 and two dashes. The dashes denote space reserved by the layout. They are not two extra order IDs.

Try =WRAPCOLS(A2:A11,4,"-") in N2. Expect four rows and three columns. The first column ends with BLK-1004, the second ends with AMN-1008, and the third contains BLK-1009 and BLK-1010 followed by two dashes. The same ten labels are placed differently. Pick the layout that matches how the next person reads or prints the labels and retain their source order.

Flatten back into the original sequence without counting padding

Return to the IDList sheet. A row-wrapped display stores the list in row-major order. Reading it back across rows returns the source values first, then the tail positions the display added. A column-wrapped display stores the list down columns, so use a column scan to reverse the placement. Changing the scan direction changes which positions are read next.

=TAKE(TOCOL(J2#,0,FALSE),ROWS(A2:A11))
=TAKE(TOCOL(N2#,0,TRUE),ROWS(A2:A11))
  1. Run the first in a free column. J2 is the row-wrapped display. TOCOL reads it row by row, with the two dashes following the source's tenth value. TAKE keeps exactly ten positions.
  2. Run the second in another free column. N2 is column-wrapped and TOCOL reads it down each column. The dashes again follow the tenth value and TAKE drops those extra positions by selecting the known length.
  3. Compare every output position to A2:A11, paying special attention to the fourth and fifth labels. They should be BLK-1004 and AMN-1005. These positions span a boundary for one of the two wrap widths.
  4. Try the wrong scan direction on a scratch copy, and count how soon it starts reading the later block. The copy may still return ten positions, but some labels will move or a pad position can be taken early.

Using the known length avoids depending on whether a display string looks blank. A padded empty string is a returned formula value, not the same thing as a genuinely unused source cell. If the original vector can itself contain meaningful blanks, confirm how those positions were represented by the wrap. The round-trip in this lab preserves the ten known nonempty IDs. It does not promise to reconstruct all worksheet types, formulas or formatting.

Without the known length, using a marker to remove padding requires an agreed rule. Suppose “-” can also be a real entry in your source, such as an explicitly labeled missing code. Removing every dash would remove both that known source entry and the newly created display padding. Check the length or keep an independent position marker before building a reversible transformation.

Detect a damaged source before calling the wrapped result clean

Return to the PastedOrders fixture. Each group is supposed to contain a text order ID, a city and a numeric amount. A wrap sees positions, so an input cell with the wrong kind of value can still be placed. The report can have four rows and three columns while including a wrong amount field. Keep a check for the content as well as one for the dimensions.

Simulate a complete list with the wrong field order

  1. In a practice copy swap the values in A3 and A4. The first three entries now read BLK-1001, 64, Pune. No positions were added or removed.
  2. Wrap the same twelve values again. C2:E2 displays BLK-1001, 64, Pune under Order ID, City, Amount. The last two fields have moved into the wrong places.
  3. Check =COUNT(CHOOSECOLS(C2#,3)). Only three of the four Amount cells are numeric. =SUM(CHOOSECOLS(C2#,3)) gives 620 because SUM ignores the text Pune in the amount array. A misleading smaller subtotal is an ordinary numerical result, not necessarily a warning from Excel.
  4. Restore A3 to Pune and A4 to 64. Require four numeric Amounts and the source total of ₹684. Then confirm the first and last rows still have the intended ID and City pairings.

A separate mistake can occur if you delete a missing entry from the middle rather than retaining its source position. If the city for BLK-1002 is simply removed from the one-column sequence, its 270 amount moves up into the City slot. The next ID moves into the Amount slot and later records shift. An incomplete final row is a visible clue, but the meaningful break began much earlier. Inspect the first wrong record instead of using the pad value to make the last row look quiet.

Run a joint shape and field check

On the clean twelve-position list, the numeric amounts are in source positions three, six, nine and twelve. You can test those positions before the wrapping step. The formula below first verifies the list length, then checks the type of each intended source amount directly. It performs that type test before array reshaping, so an empty source cell is not mistaken for a rendered zero.

=LET(sourceList,A2:A13,
     sourceCount,ROWS(sourceList),
     IF(MOD(sourceCount,3)<>0,"Check record length",
       LET(amountSlots,SEQUENCE(sourceCount/3,1,3,3),
           numericFlags,INDEX(ISNUMBER(sourceList),amountSlots),
           IF(SUM(--numericFlags)<>ROWS(amountSlots),"Check amount field positions",WRAPROWS(sourceList,3)))))

The position sequence starts at three and moves by three. ISNUMBER calculates a true or false for each original cell. INDEX selects the flags for the intended amount positions. In the normal case all four selected flags are true and the rectangular output appears. With the A3 and A4 swap, the first amount position is a city label and the formula returns the amount check message. If testing the thirteenth-value case, update the signal's source range to A2:A14 first.

This is a narrow schema check for the exercise. An incorrect amount that is still numeric can pass it. Two cities swapped between correct row positions can also pass. Check some known record keys and source totals independently. When uncertain about whether a value belongs to an ID, ask for an export that includes its labels and record key, so a proper reconciliation can compare those values.

Use a readable layout setting for a label printout

In IDList, imagine a student chooses how many labels fit across one sheet. The data still contains ten orders. Use a cell separate from the source data and result to store the desired maximum labels per row. Write Labels per row in U1 and put 3 in U2. Then enter the following expression below the control in U5 with room on its right and below.

=LET(requested,$U$2,
     IF(NOT(ISNUMBER(requested)),"Enter a number",
       IF(OR(requested<1,requested<>INT(requested)),"Enter a positive whole number",
          WRAPROWS(A2:A11,requested,"-"))))

The lab accepts a positive whole number. The wrapper uses that value as the maximum number of labels in any row. This is a presentation control. It does not change what counts as an order or filter any IDs. Test the exact cases using the same ten inputs before adding printer formats or exporting other copies.

  • Width 3. It uses four output rows, including a final row with one ID and two display dashes.
  • Width 4. It uses three rows, with two IDs and two dashes on the last row.
  • Width 5. It fills two complete rows, five IDs each, with no dashes.
  • Width 12. Ten inputs fit on a single row. The result contains ten columns, not an extra pair of padding cells.
  • Zero, fraction or text. The formula shows an input message for these choices, so they never become an unwieldy output width.

Apply borders and sizing outside the formula. If a print system specifically requires twelve cells across for ten IDs, use a separate explicit size function and label the non-order positions. The input assumption here is maximum items per row. Ask whether the printed report needs the source values alone or a fixed rectangular layout.

For one-time delivery you could copy the verified display and paste as values on a different sheet. That captures the chosen arrangement at that time. For a responsive worksheet share the formula with its source length and controls. A values-only copy won't change when the count or an ID changes. Verify the receiving user gets what their workflow needs.

Practice and answer key

Use the original values in IDList. Each calculation uses a separate scratch area with enough room. When a question asks for a number of orders, measure the source count, not the total count of all display positions. Padding displays only in some variants.

Wrap ten Order IDs into two rows of five

Use =WRAPROWS(A2:A11,5). It gives two full rows and five columns. The original fifth ID, AMN-1005, ends the first row. BLK-1006 begins the second. Confirm no positions need padding.

Wrap the same IDs into rows of four and inspect the gaps

Use =WRAPROWS(A2:A11,4). It gives three rows and four columns. The last row starts with BLK-1009 and BLK-1010 and ends with two default #N/A positions. Ten actual IDs occupy a twelve-position rectangle.

Choose a text pad for those two gaps

Use =WRAPROWS(A2:A11,4,"-"). The two extra values display dashes. A dash is a chosen display value, so do not interpret it as an unassigned order. Keep the ten items in the separate source list.

Wrap the IDs down columns of five

Use =WRAPCOLS(A2:A11,5). The result has five rows and two columns. The first column ends with AMN-1005. The second starts with BLK-1006. All ten values are present with no padding.

Count rows in the three-across output

Use =ROWS(WRAPROWS(A2:A11,3)) to get four. Three groups of three place nine values, so a fourth row is needed. The row count includes the incomplete display row.

Sum the second wrapped group of five amounts

Use =SUM(CHOOSEROWS(WRAPROWS(G2:G11,5),2)). It adds 150, 1,299, 195, 220 and 90 for ₹1,954. Those are the last five amounts of this source sequence. They are not necessarily from one city or time period.

Compare two representations of one city and amount table

In a fresh tray of cells put Pune, 544, Nashik, 360, Solapur, 1299 in A2:A7 and use =WRAPROWS(A2:A7,2). Next try Pune, Nashik, Solapur, 544, 360, 1299 with =WRAPCOLS(A2:A7,3). Both outputs show the same three cities and correctly paired amounts. The first grouping uses two consecutive fields per record. The second uses one sequence for each field.

Check the final row, source growth and export shape

Distinguish a wrap count from a required number of output columns

The setting of three in WRAPROWS says to begin another row after three input values. It does not count them or tell a colleague the product's total number of rows. Compare the original ten IDs with width three, which uses twelve positions, against width five, which uses exactly ten.

Use a layout coordinate for a label, not a category name. For example, with IDs wrapped three across, the eighth input is AMN-1008. Seven earlier positions occupied two full rows and the first cell of row three. The eighth label lands in row three, column two. In the four-down display, it is the final cell of column two. Moving the label does not change its order ID. These checks make it easier to notice when the formula used a different width than the agreed print template.

Keep the split rule when the input grows

A fixed A2:A11 formula displays ten IDs even after another has been typed below it. In a copy of IDList, convert just the ID field in A1:A11 into a Table named tblWrapIDs and put =WRAPROWS(tblWrapIDs[Order ID],3,"-") outside it. Add BLK-1011 to the new Table row. Check the Table now has eleven IDs. The fourth display row now reads BLK-1010, BLK-1011, dash. The row count remains four while the padding count drops from two to one. Compare source count and remaining pad positions along with the output dimensions.

Append AMN-1012 as the next record in that copy. It fills the last display position, so no pads remain. The following BLK-1013 starts a fifth output row, with two dashes. A report blocking that fifth row may work on twelve source items and fail exactly when a thirteenth arrives. Reserve expansion space and test around a group boundary before someone else uses the workbook.

Use the correct original shape when wrapping a whole worksheet block

WRAPROWS and WRAPCOLS require a list. If a report already has order records in several columns, wrapping the original two-dimensional data directly gives a value error. Flattening it first is possible, but that operation chooses which sequence to preserve. A 2-by-3 record array flattened by rows can be restored by wrapping three across when the values are complete and order is unchanged. If the same rectangle is flattened by columns, wrapping across three would read consecutive values with different meanings. Match the direction used to create the list.

A single vector produced by an export might contain heading information, footers or blank separators in addition to data. Identify the expected fields and remove the wrapper with a measured rule before wrapping records. If six source fields must become six columns, a vector beginning with one unexplained title line shifts every later field. Keep a few known records as a fixture so a new incoming header can be caught before any total is accepted.

Measure positions and filled values separately

On the original PastedOrders sheet, =ROWS(A2:A13) reports twelve input positions. =COUNTA(A2:A13) also reports twelve because every cell contains a value in the original fixture. These checks are interchangeable only for that complete example. Each protects against a different problem when testing new imports.

  1. On a practice copy clear A7, the numeric amount 270 for BLK-1002. Keep the cell in the range. ROWS still gives twelve and COUNTA drops to eleven. The row layout is unchanged, but one measure is absent.
  2. Use the original field check on that copy. The second intended amount flag fails. Do not infer a real zero just because a wrapped output has a blank-looking or zero-looking amount.
  3. Now enter a numeric zero into A7 under a separate agreed practice rule. Both counts return twelve and the type test succeeds. The total amount would be ₹414 because the source's original 270 was deliberately replaced with 0. The workbook should identify that as a recorded zero amount.
  4. Return to the untouched fixture and confirm the first and last rows and total ₹684. Keep the three cases together as controls for complete, missing and measured zero.

For this worksheet check the actual numeric value before applying a rupee format. The same formatting can display the clean 270 and entered zero consistently. It cannot manufacture the missing amount in the blank test copy.

For a sequence that comes from one file, store the number of input lines expected and one example of a complete record. For one that comes from separate field lists, store the expected length and order of each list. These controls explain why the wrap count is safe. A count divisible by three is only a shape check. Combined with validation of the field types and a few record keys it becomes a stronger test.

When sending an import problem back to a producer, give the original line and expected field. “Sixth source position belongs to BLK-1002 Amount” tells them exactly where to inspect. “Wrap failed” gives no starting point. Keep the pasted input and the wrapped view side by side while checking problems so the diagnosis stays traceable. If the producer can export a properly delimited row per order, adopting that explicit structure can remove the need to reconstruct it from a field sequence.

Find why a wrapped report stopped matching the source

  • A two-dimensional source is not a vector. Read the input size. One row or one column is acceptable. If you use TOCOL first, trace the first complete expected record and document whether it came from a row or a column. Converting the shape is only the first step.
  • Zeros appear in fields that should be text. An empty referenced position can materialize as a zero in an array. Go back to the source entry and decide whether to reject that record or retain a missing marker in a separate output. The default padding applies at the end and will not repair a position already included in the input.
  • The output holds the right number of cells but values shifted. Compare the first and final groups with known IDs. Look for an altered order, a missing middle field, a heading mistaken for a field, or two equal-size group blocks with records in different order.
  • A default #N/A appears at the end. Verify which positions correspond to actual data. If the list length has a remainder after dividing by the count, expect final padding. A source error in a position with real input needs its own check.
  • A spill error appears after an append. Check both dimensions of the new rectangle. A fourth row can fit and a fifth can hit the next report. Move the formula or an unrelated range on a practice copy rather than deleting data you have not identified.

Write a short report note describing whether the rectangle is a true record table or only a layout. For the record exercise, each logical row has one order whose ID, city and amount belong together. For the label sheet, each cell is a separate identifier. A row of its printout does not create a new unit for data analysis. Marking that distinction makes it easier to choose the correct source for a later lookup.

Quiz with explained answers

Which function reconstructs a list ordered as ID, City, Amount per record?

Use WRAPROWS with three values per row. It takes the first three consecutive entries for the first record and the next three for the second. This is correct only when each input group follows the same field order. Confirm the first and last IDs and their numeric amounts.

How many rows appear when ten labels are wrapped with a width of four?

Three. The first two contain four each and the last contains the remaining two. The output is a three-by-four rectangle with two pad positions. There are still ten source labels. A check that counts all twelve display positions would mistake padding for original data.

How does the formula return from a column-wrapped display to the original order?

Use a column scan when flattening N2#. Then take only as many positions as the source list had. Column scanning reads the cells in the same direction WRAPCOLS filled. Counting the source positions excludes only the control tail from this complete, nonempty test list.

What type of result appears when the wrap count is greater than the input length?

WRAPROWS returns one row and WRAPCOLS returns one column, as documented by Microsoft. The five-item input and wrap count seven display five values in a single row or column. A larger requested display shape needs a separate explicit resize step.

Why does a successful four-by-three result fail to prove the records are right?

The function followed your positions. If the source lists all IDs first but you wrap every three values as a row, several IDs can land in City and Amount. Check both field type and known ID pairings, including one boundary between groups.

Why does a missing value in the middle matter before padding the last row?

Removing a position from the middle can shift all later fields, so the record breaks long before the final pad. Preserve explicit placeholders or repair the original sequence. A pad fills only result cells left without a source position. It cannot determine where an earlier field belonged.

Interview questions you can answer from practice

How would you choose the wrap count for records pasted into one column?

I would ask what one entire record contains and inspect several boundaries in the pasted list. If each record supplies ID, City and Amount consecutively, the width is three. I would check a known final record and independently sum the measured values before making the report available.

What is the difference between padding with zero and using a text marker?

A zero can appear in calculations as a legitimate numeric observation. Text such as Not supplied makes padding visible to a human reader. In a record export, I would use the known source size or a field status rule to distinguish padding from real measurements. The right choice depends on what consumes the result.

How would you verify two independently supplied field groups describe the same orders?

I would compare their counts and a few known IDs and key-field pairs. Equal lengths do not prove their order. If there is a reliable ID next to each incoming field, I would align by that key before building the record table. I would test a deliberate swap to ensure the checks reveal it.

How can you demonstrate that a wrapped list preserves all original labels?

Compare the known source count with the wrapped shape and padding count, then flatten in the same traversal direction and take the source length. Compare every restored ID with the original sequence. In the ten-label case there should be no missing IDs before the two tail pads.

When does a values-only copy make sense?

For a static label printout, a deliberate snapshot can preserve the layout that was verified for that batch. For a workbook expected to respond to new IDs or a different print width, retain the formulas and source. I would mark the chosen width and how many labels were present at the time of export.

Revision cards

  • Start with a vector. Pass one source row or one source column.
  • WRAPROWS fills across. Its count is the maximum number of values on a result row.
  • WRAPCOLS fills down. Its count is the maximum number of values in a result column.
  • Padding fills output positions only. It is not a record or a recovered source value.
  • Check field order as well as shape. Equal lengths can still place the wrong information together.
  • Reverse the same scan. Flatten in the same direction and take the known number of original values.

Microsoft references