Cell Formula

VinNibs

New Member
Joined
Nov 27, 2013
Messages
20
Do you know what is wrong with the following

Code:
Sub CellFormula()

Dim FormulaNo As Integer
Dim i As Integer

FormulaNo = 10
i = 10

Cells(i, 13).Formula = "=" & (Cells(i, FormulaNo)) & "/" & (Cells(i, 12))

End Sub

I'm getting Applicatio-defined or object-defined error

Thanks
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Cells(i, 13).Formula

The underlined portion is giving you the error. Try this instead:

Cells(i, 13).Value
 
Upvote 0
Thanks for your response.

Cells(i, 13).Value is giving me the same error.

Anyway it needs to be a formula not value as the cell needs to change as other values in the sheet change.
 
Upvote 0
Ah, I wasn't aware that .Formula was a valid property of a range. In that case, I think you just need to tell Excel which sheet you're talking about. What is the name of the sheet this cell is in? For the next example, I'm just going to assume that it's Sheet1.

Change this:
Cells(i, 13).Formula

To this:
Worksheets("Sheet1").Cells(i, 13).Formula

If the desired worksheet is not named Sheet1, you'll have to swap out Sheet1 for the correct name of the sheet, but you WILL need to keep the quotation marks.
 
Upvote 0
Code:
Sub CellFormula()

Dim FormulaNo As Integer
Dim i As Integer

FormulaNo = 10
i = 10

Cells(i, 13).Formula = "=" & (Cells(i, FormulaNo)) & "/" & (Cells(i, 12))

End Sub

This code worked when I ran it. What is in cells J10 and L10 in your worksheet?
 
Upvote 0
Actually, try running this and see if this is what you want it to do.
Code:
Sub NewCellFormula()
    
       Range("m10").FormulaR1C1 = "=RC[-3] / RC[-1]"
    
End Sub
 
Upvote 0
Thanks bbott for your persistence I was able to edit my variables so as to allow me to use ranges, and solve the problem.

Thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top