# Create a formula based on the number of columns in Google Spreadsheets

I have two rows of data

``````Row B = price of product
Row C = quantity of producst
``````

I need to get the summary price:

``````(B1xC1)+(B2xC2)+(B3xC3)+..+(Bn+Cn)
``````

I don't know `n` (quantity of columns) because `n` is changing from time to time (from 50-1000). And I don't want to specify all `n` in formula.

Is it possible to write a summation formula for any `n`?

-

## migrated from superuser.comAug 13 '11 at 13:46

This question came from our site for computer enthusiasts and power users.

Use an array formula. ;) – Breakthrough Jul 5 '11 at 13:22

Use `sumproduct()`.

You could either change the formula every time (only involves changing two cells) or just go with:

``````=sumproduct(B1:B1000,C1:C1000)
``````

Alternatively you could write yourself a short function that:

1. Checks the two input ranges are the same size
2. Calculates the size of the ranges (say 'X')
3. Runs a loop 'X' times multiplying the two range values together and maintaining a total

However I would just use `sumproduct()`.

-

If you do not want to make the formula depending on the number of rows, you can simply use the formula `=SUMPRODUCT(B:B;C:C)`. If you have a more elaborated need, please let me know.

-