View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
JLatham JLatham is offline
external usenet poster
 
Posts: 2,203
Default VBA formula question

Yes, you can, but sometimes it takes a lot of code to emulate a worksheet
function. For example, in the case you gave, you'd have to test for 0 in M13
(R4C13) and make the decision in your code whether to put up "Data?" or the
result.

One trick you can use would be to achieve the result would be to add a line
of code right after the one where you've used your .FormulaR1C1 = ...
statement
cell.offset(,-1).Formula = cell.offset(,-1).Value
would take the result of the formula you just put into a cell and replace
the formula with that result/value. Of course the tradeoff here is that it
takes more time to process than just building the formula, or possibly more
time than having the calculation done in code.

"Roger on Excel" wrote:

I use the following type of code to place formulas in cells :

For Each cell In Range("A1:A2")

cell.Offset(, 1).FormulaR1C1 = "=RC[10]&RC[8]&RC[15]"
'Equiv
cell.Offset(, -1).FormulaR1C1 =
"=IF(ISERROR(RC[5]/r4c13),""Data?"",RC[3]/r4c13)"
Next

This works fine, however my question is whether one can perform the
calculations in the code and just place the result into the cells?

I guess this may require learning a whole new syntax for coding the
equations, but I was wondering if someone could give me some pointers?

Many Thanks,

Roger