Writing Excel formulas in macros - Help!!

M

Mark Lucas

I am attempting to use a macro to write a formula in to a blank cell. When
the formula is written, the cell reference in the formula has apostrophes
around it so it won't function. I'm sure there is a syntax step I'm missing.
Can someone help??
 
D

Dave Peterson

I bet you're using .formulaR1C1, but you're creating the formula using A1
reference style.

Maybe you can use .formula (or actually change the formula so that it uses R1C1
reference style).

somecell.formulaR1C1 = "=...."
becomes
somecell.formula = "=...."
 
M

Mark Lucas

ActiveCell.FormulaR1C1 = "=IF(MID(b2,8,3)=""CFD"",1,0)" is the code in the
macro

=IF(MID('b2',8,3)="CFD",1,0) is what shows up in the cell.
 
M

Mark Lucas

Wait there's more ... I want the cell reference to change as that formula is
being copied down the column. How can I write that in to the formula?
 
D

Dave Peterson

Try toggle the setting and looking at a few formulas:

tools|options|General tab|check R1C1 reference style.

You'll notice that the columns are now numbered--not lettered.

And your formulas look like:

=if(r2c1="asdf","ok","not ok")
instead of
=if($A$2="asdf","ok","not ok")

Remember to toggle this setting back.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top