Question: please help me to make a query using mysql to show the products(product.name) that have total quantity SUM(goods.quantity) > 100 in all 5 warehouses, specifying the warehouse(warehouse.name) with the greatest quantity of each product. Here is the scheme of all tables for better understanding:

[image of warehouses database](https://i.stack.imgur.com/YKvMS.png)

I am trying this query:

```
SELECT product.name AS product, warehouse.name AS warehouse 
FROM product, warehouse, goods 
WHERE goods.productId = product.id AND goods.warehouseId = warehouse.id 
GROUP BY product.name, warehouse.name 
HAVING SUM(goods.quantity)>100;

```

it gives me the following:

| product | warehouse |
| --- | --- |
| engine | New York |
| right back door | New York |
| main frame | London |
| left back door | London |
| back lights | London |
| steering wheel | Sydney |
| wheel | Toronto |
| car cleaner | Toronto |

But it only shows products that have `goods.quantity > 100`, not the `SUM(goods.quantity) > 100` in all warehouses.

Besides, it doesn't show warehouse with the greatest quantity of each product, but any warehouse that has this product.

Answer: 
Looks like you're just missing a double quote after the last ampersand:

```
Cells(i, 8).Formula = "=left(A" & i & ", 2")

```

When concatenating you should end up with an even number of quotation marks.
Write Formulas to Dynamic Range
-------------------------------

```vb
Option Explicit

Sub writeFormulaToRange()
    ' Source First Cell Address
    Const srcFirstCell As String = "A1"
    ' Destination Column ID (Number or String)
    Const dstColID As Variant = "K" ' or 11
    ' Declare Variables.
    Dim srcRng As Range ' Source Range
    Dim srcLastRow As Long ' Source Last Row
    Dim ColOffset As Long ' Column Offset
    ' Define First Cell Range.
    With Range(srcFirstCell)
        ' Source Column is the column of the First Cell Range.
        ' Source Last Row is the row of the Last Non-Empty Cell Range
        ' in Source Column.
        ' Calculate Source Last Row.
        srcLastRow = .Worksheet.Cells(.Worksheet.Rows.Count, .Column) _
            .End(xlUp).Row
        ' Source Range is the range from the First Cell Range
        ' to the Last Non-Empty Cell Range.
        ' Define Source Range.
        Set srcRng = .Resize(srcLastRow - .Row + 1)
        ' Column Offset is the difference between the Destination
        ' and the Source Column Numbers.
        ' Calculate Column Offset.
        ColOffset = .Worksheet.Columns(dstColID).Column - .Column
    End With
    ' Destination Range is of the same size as the Source Range.
    ' Write the formulas to the Destination Range.
    srcRng.Offset(, ColOffset).Value = "=Left(" & srcFirstCell & ",2)"
    ' or:
    'rng.Offset(, ColOffset).Formula = "=Left(" & srcFirstCell & ",2)"
End Sub

```