# Multiply numbers in each row with remaining rows in cell range (UDF)

This article demonstrates a User Defined Function (UDF) that multiplies numbers in each row with the remaining rows in a cell range.

The image above demonstrates the UDF in cell range B5:D8, it takes the first row and multiplies the numbers with all rows in the same cell range. It then continues to the second row and multiplies that row with all rows in the cell range.

The total number of rows returned is calculated like this: number_of_rows * number_of_rows. There are two rows in cell range B2:D3 and the UDF returns four rows (2*2 = 4).

**What's on this page**

Hello Oscar,I have an Excel dataset consisting of 500 rows by 7 columns. I need to generate additional data points from this dataset.I want to multiply (or other function) each row by all 500 rows, creating 250,000 new rows of data.Each cell needs to function as a constant that is multiplied by all the other cells in the same column (which are not acting as constants).How do I do this efficiently? Thanks in advance!

### Answer

I don't think you can multiply cells like that with an array formula, as far as I know.

Below is a User Defined function I made, it multiplies each row by all other rows in the specified range.

## 1. MultiplyCells - User Defined Function

The picture above shows you the udf using values in cell range B2:B3 and returns the calculated values to cell range B5:D8.

This is a UDF (custom function), you need to copy code to the code module before you can use it.

### UDF syntax

*range*)

### Arguments

There is only one argument in this UDF.

*range* - Cell range

## 2. VBA Code

'Name User Defined Function and dimension parameters Function MultiplyCells(rng As Range) 'Dimension variables and declare data types Dim rng1 As Variant Dim tbl() As Variant Dim rr As Single, r As Single, c As Single, tr As Single 'Save values in range object rng to array variable rng1 rng1 = rng.Value 'Redimension array variable tbl based on rows and columns in range object rng ReDim tbl(1 To rng.Cells.Rows.CountLarge ^ 2, 1 To rng.Cells.Columns.CountLarge) tr = 1 'For ... Next statements For rr = LBound(rng1, 1) To UBound(rng1, 1) For r = LBound(rng1, 1) To UBound(rng1, 1) For c = LBound(rng1, 2) To UBound(rng1, 2) 'Multiply values and save the product to array variable tbl tbl(tr, c) = rng1(rr, c) * rng1(r, c) Next c 'Add 1 to variable tr and save to variable tr tr = tr + 1 Next r Next rr 'Return numbers in array variable tbl to worksheet MultiplyCells = tbl End Function

## 3. How to enter the UDF as an array formula

Enter this UDF as an array formula, if the range has 2 rows and 3 columns enter the UDF in a cell range with 4 (2*2) rows and 3 columns.

Here are the steps to enter this UDF as an array formula:

- Select cell range B5:D8.
- Type the UDF name and argument:
=MultiplyCells(B2:D3)
- Press and hold CTRL + SHIFT simultaneously.
- Press Enter once.
- Release all keys.

If you did it right the formula now has a curly bracket before and after. Like this {=MultiplyCells(B2:D3)}. Don't enter these yourself. If you are unsure, watch the formula bar carefully in the animated picture above.

## 4. Where to put the code?

- Press Alt+ F11 to open the Visual Basic Editor (VBE).
- Press with mouse on "Insert" on the top menu, see the image above.
- Press with mouse on "Module" to insert a module to your workbook.
- Copy VBA code above.
- Paste to code window, see the image above.
- Exit VBE and return to Excel.

Copy the following code to a module. How to insert a module to a workbook.

## 5. Explaining VBA code

The code explained here is located in the code module. You can find the code module in the VB Editor, press Alt+F11 to open the VB Editor. Press with left mouse button on "Module" found in project explorer.

**Function name and arguments**

A User Defined Function always starts with "Function" and then a name. This UDF has only one argument. Variable rng is a range object, read more about Defining data types.

Function MultiplyCells(rng As Range)

**Declaring variables**

tbl() is an array and a variant variable, tbl has two parentheses meaning it is an array. rng1 is also a variant. rr, r, c, and tr are Single variables. Read more about Defining data types.

Dim rng1 As Variant Dim tbl() As Variant Dim rr As Single, r As Single, c As Single, tr As Single

**Save values from rng (range object) to rng1 (variant)**

This speeds up the function considerably if you are working with large cell ranges. Excel copies all the values from the sheet and puts them in memory (array).

rng1 = rng.Value

**Build array**

ReDim dimensions the tbl array variant, it has the number of rows of the range argument with the power of 2. The values in the array are numbered from 1 to n. There are as many columns as in the range argument.

ReDimtbl(1 To rng.Cells.Rows.CountLarge ^ 2, 1 Torng.Cells.Columns.CountLarge)

**Use variables r and c to save values in array**

Variable tr keeps track of where to save the next column values in tbl.

tr = 1

**For ... Next statement**

Repeats a group of statements a specified number of times, here we want to multiply each row by all rows.

For rr = LBound(rng1, 1) To UBound(rng1, 1) For r = LBound(rng1, 1) To UBound(rng1, 1) For c = LBound(rng1, 2) To UBound(rng1, 2)

**Save value to tbl array**

The variables tr, c, rr, and r help us keep track of which values to use and where to save.

tbl(tr, c) = rng1(rr, c) * rng1(r, c)

**Add 1 to variable tr**

tr = tr + 1

**Return tbl values to function**

MultiplyCells = tbl

**End udf**

A function procedure ends with this statement.

End Function

This article demonstrates a macro that copies values between sheets. I am using the invoice template workbook. This macro copies […]

Open Excel files in a folder [VBA]

This tutorial shows you how to list excel files in a specific folder and create adjacent checkboxes, using VBA. The […]

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 […]

Working with COMBO BOXES [Form Controls]

This blog post demonstrates how to create, populate and change comboboxes (form control) programmatically. Form controls are not as flexible […]

Identify missing numbers in a column

The image above shows an array formula in cell D6 that extracts missing numbers i cell range B3:B7, the lower […]

This workbook contains two worksheets, one worksheet shows a calendar and the other worksheet is used to store events. The […]

Change chart data range using a Drop Down List [VBA]

In this article I will demonstrate how to quickly change chart data range utilizing a combobox (drop-down list). The above […]

Run a Macro from a Drop Down list [VBA]

This article demonstrates how to run a VBA macro using a Drop Down list. The Drop Down list contains two […]

What's on this page Copy a file Copy and rename a file Rename a file List files in a folder […]

How to create an interactive Excel chart [VBA]

This article describes how to create an interactive chart, the user may press with left mouse button on a button […]

### 2 Responses to “Multiply numbers in each row with remaining rows in cell range (UDF)”

### Leave a Reply to Alex Groberman

### 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

If I understand the question correctly, this function would work:

First you would put your original 500 rows in a table somewhere ("Table1"), and then enter this function as an array (ctrl-shift-enter) starting in cell A1, with seven columns and 250,000 rows. It can be adapted with subtraction from row() and column() if you wish to put your data in a location other than cell A1.

-Alex

Alex,

Great comment!

It works fine, why didn't I think of this.

Your formula is small and genius.

Thank you for commenting.