## Split values equally into groups

**Question:**How do I divide values equally into groups (3 lists or less)?

This post shows you two different approaches, an array formula, and a User Defined Function. You will find the UDF later in this post.

The above picture shows you values in column A and they are equally split across 9 columns, column D to M.

Formula in D5:

Now copy cell D5 and paste to cells below and to the right.

### Explaining formula in cell D5

#### Step - 1 Keep track of lists

The COLUMNS function works just like the ROWS function except it counts columns instead of rows in a cell reference.

Cell |
COLUMNS function |
Result |

D5 | COLUMNS($A$1:A1) | 1 |

E5 | COLUMNS($A$1:A2) | 2 |

F5 | COLUMNS($A$1:A3) | 3 |

Cell E2 contains the number of lists to put the values into.

$E$2>=COLUMNS($A$1:A1)

becomes

9>=1

and returns TRUE. This makes the formula return blank cells when the number of lists are greater than the value in cell E2.

#### Step 2 - Return blanks if all lists are populated

The IF function lets you specify a value if the logical expression returns TRUE (argument 2) and another value if FALSE (argument 3).

IF($E$2>=COLUMNS($A$1:A1),INDEX($B$3:$B$102,(ROWS($A$1:A1))*$E$2-($E$2-COLUMNS($A$1:A1))),"")

becomes

IF(TRUE,INDEX($B$3:$B$102,(ROWS($A$1:A1))*$E$2-($E$2-COLUMNS($A$1:A1))),"")

and returns

INDEX($B$3:$B$102,(ROWS($A$1:A1))*$E$2-($E$2-COLUMNS($A$1:A1)))

#### Step 3 - Return number based on an expanding cell reference

The ROWS function returns the number of rows in a cell reference, this cell ref is special. It expands as the formula is copied to cells below.

ROWS($A$1:A1)

returns 1.

Cell |
ROWS function |
Result |

B3 | ROWS($A$1:A1) | 1 |

B4 | ROWS($A$1:A2) | 2 |

B5 | ROWS($A$1:A3) | 3 |

#### Step 4 - Multiply with cell E2

ROWS($A$1:A1)*$E$2

becomes

1*9 equals 9.

#### Step 5 - Subtract $E$2 with COLUMNS

$E$2-COLUMNS($A$1:A1))

becomes

9-1 equals 8

#### Step 6 - Calculate row number

ROWS($A$1:A1)*$E$2-($E$2-COLUMNS($A$1:A1))

becomes

9-8 equals 1.

#### Step 6 - Get value based on row number

The INDEX function returns a value in cell range based on a row and column number. This is a single column cell ref so the column number is not neccessary in this case.

INDEX($B$3:$B$102,(ROWS($A$1:A1))*$E$2-($E$2-COLUMNS($A$1:A1)))

becomes

INDEX($B$3:$B$102,1)

and returns "YM" in cell D5.

### User defined function

You decide how many groups you want by selecting a cell range with as many columns as you want groups and then enter the UDF. It is designed to group values depending on how many columns you have selected before entering it.

The animated picture above shows you a cell range with 5 columns.

This post shows you how to categorize values into worksheets:

Rearrange values based on category [VBA]

In this post I am going to rearrange values from a list into unique columns. Before: After: The code Download […]

Rearrange values based on category [VBA]

and this post shows you how to categorize values to columns based on a condition:

**How to enter an array formula**

- Select cell range C2:G11
- Paste above array formula to your formula bar

- Press and hold CTRL and SHIFT keys simultaneously
- Press Enter once

The formula in the formula bar now looks like this: {=GroupValues(A2:A24)}

Don't enter these curly parentheses yourself, they appear automatically if you did the above steps correctly.

Array formulas allows you to do advanced calculations not possible with regular formulas.

**VBA code**

Function GroupValues(rng As Range) Dim result As Variant c = Application.Caller.Columns.Count r = Application.Caller.Rows.Count ReDim result(1 To r, 1 To c) i = 1 For ro = 1 To r For co = 1 To c If rng.Cells(i) <> "" Then result(ro, co) = rng.Cells(i) Else result(ro, co) = "" End If i = i + 1 Next co Next ro GroupValues = result End Function

**How do I copy the code to my workbook?**

- Open VB editor (Alt+F11)
- Insert a module to your workbook
- Paste code to code module

- Go back to Excel

Split data across multiple sheets [VBA]

In this post I am going to show how to create a new sheet for each airplane using vba. The […]

Text to columns: Split words in a cell [Array formula]

This blog article describes how to split strings in a cell with space as a delimiting character, like Text to […]

Rearrange values based on category [VBA]

In this post I am going to rearrange values from a list into unique columns. Before: After: The code Download […]

How to count word frequency in a cell range [UDF]

This user defined function creates a unique distinct list of words and how many times they occur in the selected […]

Extract unique distinct values from a filtered Excel defined Table [UDF and Formula]

Robert Jr asks: Oscar, I am using the VBA code & FilterUniqueSort array to generate unique lists that drive Selection […]

List files in a folder and subfolders [UDF]

This article demonstrates a user defined function that lists files in a ggiven folder and subfolders. A user defined function is […]

Split words in a cell range into a cell each [UDF]

This post describes how to split words in a cell range into a cell each using a custom function. I […]

### 13 Responses to “Split values equally into groups”

### Leave a Reply

### How to comment

**How to add a formula to your comment**

<code>Insert your formula here.</code>

**Convert less than and larger than signs**

Use html character entities instead of less than and larger than signs.

< becomes < and > becomes >

**How to add VBA code to your comment**

[vb 1="vbnet" language=","]

Put your VBA code here.

[/vb]

**How to add a picture to your comment:**

Upload picture to postimage.org or imgur

Paste image link to your comment.

**Contact Oscar**

You can contact me through this contact form

i tried to add a list number 4 with this formula =IF(D1>3,INDEX($A$2:$A$21,(ROW($A$2:$A$21)-MIN(ROW($A$2:$A$21))+1)*D1-(D1-4)),"")

the value at f4 is DD but the f5, f6,etc....arejust repeating the result

Fahmy,

split-values-into-groups-using-excel-formula.xlsx

Hi Oscar, thanks for the great tutorial.

I was hoping to use what you've described above but for data that stretches across a single row.

For example, if I have data that stretches from A1:J1, I would like to split it up into 5 rows so that I would have the values appear in A2:B2, A3:B3, A4:B4,A5:B5, A6:B6.

I have taken a stab at manipulating the formulas you've provided but with no luck. Was hoping you could help out!

Thanks!

Hasan,

Array formula in cell range: A2:E3:

A2:E3))+1)+(ROWS(A2:E3)*(COLUMN()-1))),"")How to create array formula

1. Select cell range A2:E3

2. Paste formula in formula bar

3. Press and hold Ctrl + Shift

4. Press Enter

Adjust bolded cell ranges if you enter the array formula in a different cell range.

Hasan1.xlsx

HI Oscar! I would need your help to strech column A to 500 rows with 8 lists. Thanks!

Hey, quick question, would this beable to expand into say a list of 100 values and have 10 groups. ive tried editing the code but am unsuccessful in getting the formula to work correctly

Kerien

Here is a workbook for you:

split-values-into-groups-using-excel-formulav2.xlsx

Hi Oscar, I wonder if you could help, I have a large number of record numbers (basically a call list for a sales office) which I need to split up into equal lists, there could be between 100 and about 400 items.

I have tried to use your example spreadsheet and replace the ranges with dynamic named ranges (which are set up contain all the record numbers), but that gives me a #VALUE error, even if I use crtl+alt+enter.

In addition, I cannot seem to make the lists in your example longer to accommodate this, if I copy and paste the formula downward, I get one record number repeated.

Here is a link to my spreadsheet on google drive (I am using excel 2013 - just using google drive to host the file!) I would really appreciate it if you could have a look and let me know where I'm going wrong...

Link

Cheers!

Joe,

I have built a user defined function that is easier to use, read this post again.

Hi Oscar, Thank you so much, this is much easier, thankyou!

Hi Oscar! Thank you so much for this tutorial. I have been trying to replicate this exact workbook for 10 groups, and also use the "CHOOSE" AND "RANDBETWEEN" functions to randomize the values of column A into the array but having a bit of trouble doing so. Would this be possible to set this up? Really appreciate your help!

I am trying to use your formula (downloaded the spreadsheet) to create divide 253 students (value) into 15 groups equally. I tried to edit the code...but i'm getting an error message when trying to adjust the array.

Tamara Smith,

If you are trying to expand the array formula and get an error, try this:

The array formula in column C is entered in cell range C4:C25.

Select cell your new cell range, example C4:C27

Click in formula bar.

Press and hold CTRL + SHIFT. Press Enter to create a new array formula for your new range.

I recommend you use the User Defined Function, it is much easier to work with.