2

I am trying to create a macro that links a cell from another worksheet (this is stored as a variable called SheetName). The user is prompted with an Input box to select a cell. I would like to have a cell in another worksheet reference to the selected cell.

Here is the relevant code:

Dim WorkRng As Range
    Total1 = "Select Total cell"
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Range", Total1, WorkRng.Address, Type:=8)
    Worksheets("WorksheetA").Range("C6").formula = "=" & 'SheetName.Name' & "!" & WorkRng.Address

The last line is where I am running into object errors. Any help is greatly appreciated!

2 Answers 2

3

Try,

Worksheets("WorksheetA").Range("C6").formula = _
   "='" & SheetName.Name & "'!" & WorkRng.Address

'note the quote here "='" and here "'!"

Sign up to request clarification or add additional context in comments.

Comments

1

Where is sheetname defined? It then needs to be concatenated into the string

DIm SheetName As Worksheet
Set SheetName = Worksheets("Sheet2")

formula then

"=" & SheetName.Name  & "!" & [A1:B2].Address

2 Comments

External includes the worksheet name; in fact it also includes the workbook name and path which Excel evaporates away if unnecessary. You might be duplicating the worksheet name.
Aghhhh... thanks for letting me know. Was questioning myself. Should have just done some quick homework.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.