简体   繁体   中英

Excel VBA: Formula with reference to cell in another worksheet (variable)

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!

Try,

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM