cells.formula

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
Why I need cells.formula when I can do it without cells.formula as following:
Code:
Sub cell_formula()
    Workbooks(1).Worksheets(1).Cells(1, 1).Value = "=sum(c1+c2)"
End Sub
Thank you so much.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Re: cells.fromula

Hi
Why I need cells.formula when I can do it without cells.formula as following:
Code:
Sub cell_formula()
    Workbooks(1).Worksheets(1).Cells(1, 1).Value = "=sum(c1+c2)"
End Sub
Thank you so much.

Excel interprets it as a formula, it can be:

Code:
Workbooks(1).Worksheets(1).Cells(1, 1).Formula = "=sum(c1+c2)"
Workbooks(1).Worksheets(1).Cells(1, 1).Value = "=sum(c1+c2)"
'Or
Workbooks(1).Worksheets(1).Cells(1, 1) = "=sum(c1+c2)"    'The default property of range is value

But can not be
Code:
Range("A1").FormulaR1C1  = "=sum(c1+c2)" 'Excel could interpret different
 
Upvote 0
As an example (as I have requested the link be removed from the previous post as a possible issue with the site).

Put the below in a spreadsheet


Excel 2010
ABCD
1910.63
2125.65
3784.98
Sheet3
Cell Formulas
RangeFormula
D1=SUM(C2:C3)


then run the code below and see the result in the Immediate window
Code:
Sub TestFormVal()
Debug.Print Range("D1").Value
Debug.Print Range("D1").Formula
End Sub
 
Upvote 0
@MARRK858
I'm not getting any problems or warnings regarding John's site, so have left the link.
 
Upvote 0
Re: cells.fromula

Hi

You need it to read the formula.

Can you clarify, pgc? This does exactly what I'd expect

Code:
  With Range("A2")
    .Value = "=sum(b2,c2)"
    Debug.Print .Formula
  End With
 
Upvote 0
Re: cells.fromula

Can you clarify, pgc? This does exactly what I'd expect

Code:
  With Range("A2")
    .Value = "=sum(b2,c2)"
    [B][COLOR="#FF0000"]Debug.Print .Formula[/COLOR][/B]
  End With
I think that is what pgc was saying... if you want to read (back) the formula that is in the cell, you need to use the Format property to get the text representation of the formula.
 
Upvote 0
Hi

Sorry, I guess I was not very clear.

lezawang asked why do we need the .Formula property in the Range object.
It seemed in the example in post #1 that the .Value was enough for writing both values and formulas.

What I answered is that, in fact, you don't need the .Formula property to write a formula (you can use .Value), but you need it to read the formula back.

Writing with .Value and .Formula is equivalent.
Reading with .Value and .Formula is not equivalent. The latter allows you to get the formula in the cell while the former reads the cell value (the result of the evaluation of the formula).

You need the .Formula property to read back the formula in the cell.

Hope it's clear.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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