# Thread: Referring to cells pt 2 (XL97/WinNT4)

1. ## Referring to cells pt 2 (XL97/WinNT4)

Could someone tell me how I go about referring to a cell in a formula that is only going to be set when the code is run (and will change each time it is run!)?

That is, I have this line of code:

.Cells(intNStartRow + 6, 6).Formula = "=IF(E11=""-"",""-"", ((1+E11/200)^2-1)*100)"

But E11 should be replaced with whichever cell would be covered by intNStartRow + 6, 5 - that is, it's calculating on the value of the cell to its left, bearing in mind that if it's a - (hyphen) then it should ignore the calculation and just put another hyphen in.

I'd be grateful if someone would either point me in the right direction if it's possible, or indicate how to do it in code if it's not!

Ta muchly

2. ## Re: Referring to cells pt 2 (XL97/WinNT4)

Beryl,
The easiest way is to use something like:
.Cells(intNStartRow + 6, 6).FormulaR1C1 = "=IF(RC[-1]=""-"",""-"", ((1+RC[-1]/200)^2-1)*100)"
Hope that helps.

3. ## Re: Referring to cells pt 2 (XL97/WinNT4)

Umm - don't worry, peeps, I had a rush of something to the brain and worked it out for myself - I needed to break the code with the variable, like so:

.Cells(intNStartRow + 6, 6).Formula = "=IF(" + .Cells(intNStartRow + 6, 5).Address + "=""-"",""-"", ((1+" + .Cells(intNStartRow + 6, 5).Address + "/200)^2-1)*100)"

I don't know, maybe this will be useful to someone else!

4. ## Re: Referring to cells pt 2 (XL97/WinNT4)

Thanks, Rory - I had worked out a (far less elegant) solution for myself, which I was just typing as you posted your one - but I think I prefer yours, so I'm going to use that ...

<img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•