Reputation: 1496
I am trying to write a macro to set the value of a cell that refers to another worksheet as a variable:
Dim worksheetName as String
worksheetName = "Pricing Sheet"
Worksheets("Main Sheet").Cells(11, 4).Value = "='worksheetName'!" & "A1"
HOWEVER, the result is the cell ends up equaling "='worksheetName'!" & "A1" WHEREAS the end result I want is the cell equaling Worksheets("Main Sheet").Cells(11, 4).Value = "='Pricing Sheet'!" & "A1"
The variable worksheetName could take on multiple values, not just "Pricing Sheet" therefore I need it to be a variable in the cell.
Please help! Thanks!
Upvotes: 1
Views: 2010
Reputation: 6433
Use Formula, not Value. I guess you should use somthing like this for dynamic variable.
worksheetName = Worksheets("Main Sheet").Cells(R,C).Value
Worksheets("Main Sheet").Cells(11, 4).Formula = "='" & worksheetName & "'!" & "A1"
Upvotes: 1