简体   繁体   中英

Link formula using cell's value

I have a very simple question.

I want to get values from another workbook. A simple way to do this is to enter a formula like this in a cell:

='C:\Path\[Workbook.xlsm]Sheet1'!$D$4

This is working well. It returns the value in cell D4 in sheet1 in the workbook.xlsm in path directory.

As I have to treat multiple values and the path/name can change I want to stock the path/name in a cell and reuse them everywhere I need.

in OpenWb.xlsm, Sheet_Report, D9: 
    value: C:\Path\
    Name: Path
in OpenWb.xlsm, Sheet_Report, D10: 
    value: Workbook.xlsm
    Name: WbName

To use these value I wrote:

='Path[WbName]Sheet1'!$D$4

Excel gives an incorrect adress error add a [:

='[Path[WbName]Sheet1'!$D$4

When I remove the [, Excel open a file dialog box where I can select the path, name and then the sheet. It then works but it is annoying to go through these filedialog box.

How can I write the formula to go to another workbooks using cell's value ?

You would need to use INDIRECT

=INDIRECT("'"&Path&"["&WbName&"]Sheet1'!$D$4")

but that will only work if the source workbook is open (in which case you don't need the path!). If you have the MOREFUNC add-in installed it has an INDIRECT.EXT function which is supposed to work with closed workbooks (I don't use it so can't comment from experience)

I simply used excel for macro:

the syntax is very simple and can be genralised to range with simple loops.

 Arg = "'" & Path & "[" & File & "]" & Sheet & "'!" & "R4C4" 'Range(Ref.Range("A1").Address(, , xlR1C1))C
     'Execute XLM macro
    GetValue = ExecuteExcel4Macro(Arg)

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