1

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?

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.