# Excel VBA Insert Formula Into Cell using a variable

Hello Experts,

This should be easy but I can not get the following to work.

I'm entering a formula into a cell using a variable in the formula.  What I need to do is insert a formula that gives the average of C2 to N , where N is an entry the user puts in.  Soooo, the formula that gets posted into F1 is: Range("F1").Formula = "=Average("\$C\$3" ":" "\$C,\$N" )"  I am fairly new to Excel VBA.  Thanks!!
LVL 2
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Older than dirtCommented:
Range("F1").Formula = "=Average("\$C\$3:\$C" & N)
Older than dirtCommented:
BTW you said C2 but your example shows C3, so I went with that.
EngineerCommented:
Try

Range("F1").Formula = "=Average(\$C\$3:\$C\$" & N & ")"

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Thanks for the quick response!

Martin I tried your formula and received an error at the first \$, "Invalid Character"

Saqib I tried your formula and this is the result in F1:  =AVERAGE("\$C\$2:\$C\$"&n )
EngineerCommented:
I have modified my formula. Give it a go again.
Author Commented:
Saqib that worked!!  Thanks!!
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.