VBA Formula Assistance

jstevens
GoldLounger
Posts: 2631
Joined: 26 Jan 2010, 16:31
Location: Southern California

VBA Formula Assistance

Post by jstevens »

I'm having a challenge with writing a formula to a cell via code:

Example:
Sheets("Sheet1").Range("A1").Formula = "="Some text here: " & Sheet2!C2"

I'm receiving a compile error: syntax error

I have tried adding additional quote marks to the end of the statement but to no avail.

Thanks for taking a look,
John
Regards,
John

User avatar
HansV
Administrator
Posts: 78512
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Formula Assistance

Post by HansV »

It should be

Sheets("Sheet1").Range("A1").Formula = "=Some text here: " & Sheet2!C2
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2631
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: VBA Formula Assistance

Post by jstevens »

Hans,

I'm now coming up with another error:

run-time error 424: object required

Regards,
John
Regards,
John

User avatar
HansV
Administrator
Posts: 78512
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Formula Assistance

Post by HansV »

Sorry, my mistake. I didn't notice that you hadn't used VBA for the last part.

Sheets("Sheet1").Range("A1").Formula = "=Some text here: " & Worksheets("Sheet2").Range("C2")
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2631
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: VBA Formula Assistance

Post by jstevens »

I'm having one of those days...

Thank you,
John
Regards,
John

Reimer
3StarLounger
Posts: 233
Joined: 10 Feb 2010, 19:17

Re: VBA Formula Assistance

Post by Reimer »

Me too.
I was going to reply but could not get it to work, so I went to break.
On the way there I remembered -All you have to do is Record a macro and perform the steps. Then look at the macro and tweak.
(Just in case Hans is not avaiable).
Chuck Reimer
(I'm from the Government and I'm here to help) ;-)

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: VBA Formula Assistance

Post by rory »

I would have thought:
Sheets("Sheet1").Range("A1").Formula = "=""Some text here: ""&Sheet2!C2"
Regards,
Rory

User avatar
HansV
Administrator
Posts: 78512
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Formula Assistance

Post by HansV »

You're correct, I wasn't paying attention.
Best wishes,
Hans