Hi I want to insert formula into a cell by determining variables for each value.

Eg.

```
=G5/D5
```

So in cell

```
Sheets("Sheet1").cells(LastRow + 2, 2).value = "=columnletter1 + (lastrow + 2)/columnletter2 + (lastrow + 2)
```

I find the columnletter is G by determining if the column name is X and the row is LastRow + 2.

How do I write so that I get “=G5/D5” returned into the cell?

Not sure if I understand your requirements exactly, I think you mean:

`columnletter1`

is a string, = `G`

`columnletter2`

is a string, = `D`

`lastrow`

is a long, = `3`

To set a formula, construct a string and write to the `.Formula`

property

```
Sheets("Sheet1").cells(LastRow + 2, 2).Formula = _
"=" & columnletter1 & lastrow + 2 & "/" & columnletter2 & lastrow + 2
```

### Answer：

For your matrix calculations below, the given code including the formula with variables may be helpful.

```
For ROW= 0 To K - 1
For COL= 0 To J - 1
Range("G5").Offset(ROW, COL).Select
ActiveCell.FormulaR1C1 = _
"=SUMIFS(R2C5:R500C5,R2C4:R500C4,RC[" & -COL- 1 & "],R2C2:R500C2,R[" & -ROW- 3 & "]C,R2C3:R500C3,R[" & -ROW- 2 & "]C)"
Next
COL= COL + 1
Next
ROW= ROW + 1
```

Tags: variables, variablesexcel, vba