VBA syntax to insert a formula to link to another worksheet cell

ebrandt

Board Regular
Joined
Dec 13, 2010
Messages
54
trying to insert a formula to link a cell to another worksheet in my workbook, which I I’m have assigned a text variable “Sheet_Name”.

This is the code I have written………………..


Range("F13").Select

ActiveCell.FormulaR1C1 = "=’" & Sheet_Name & "'!D13"



This is the result I get which produces an error……………………………..

=@'03.00 - Building Concrete'!'D13'

What am I doing wrong?
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
It looks like you have a slanted quote mark in your formula ’ instead of the straight up and down one of '
Excel does not like slanted ones.

Also, no need to select a cell before applying a formula to it, and remove the R1C1 reference, as you are not using relative ranges in your formula.
Try:
VBA Code:
Range("F13").Formula = "='" & Sheet_Name & "'!D13"
 
Upvote 0
Solution
It looks like you have a slanted quote mark in your formula ’ instead of the straight up and down one of '
Excel does not like slanted ones.

Also, no need to select a cell before applying a formula to it, and remove the R1C1 reference, as you are not using relative ranges in your formula.
Try:
VBA Code:
Range("F13").Formula = "='" & Sheet_Name & "'!D13"
That worked.

Why doesn't this...............

Range("H13").Formula = "=IF(" & Sheet_Name & "!F13>1," & Sheet_Name & "!F13,x)"
 
Upvote 0
That worked.

Why doesn't this...............

Range("H13").Formula = "=IF(" & Sheet_Name & "!F13>1," & Sheet_Name & "!F13,x)"
What is the value of Sheet_Name?
And what is the "x" at the end of your equation?
What is supposed to be happening there?

Here is a little hint. If you know what the formula is supposed to look like on the sheet (for just one case/example), turn on your Macro Recorder, and record yourself entering it into the sheet. Then view the code and see what it looks like.

Then, you can check to see if what you are building matches that with a simple Message Box, i.e. take the formula from here:
VBA Code:
Range("H13").Formula = "=IF(" & Sheet_Name & "!F13>1," & Sheet_Name & "!F13,x)"
and return it via a Message Box:
VBA Code:
MsgBox "=IF(" & Sheet_Name & "!F13>1," & Sheet_Name & "!F13,x)"
 
Upvote 0

Forum statistics

Threads
1,214,825
Messages
6,121,787
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top