Refer to Excel workbook by path, based on cell value data

前端 未结 3 1588
旧巷少年郎
旧巷少年郎 2021-01-06 02:15

I have an Excel sheet that draws data from other, closed Excel workbooks. Currently it works fine when I list out the closed workbook\'s entire path, but I\'d like to use a

相关标签:
3条回答
  • 2021-01-06 02:47

    You can store a full reference including the file path to a range in a closed file in a name in excel (either directly or via VBA based on selections in different cells and using the Worksheet_Change procedure as above) and then refer to the file using the name in a formula as normal. This gets over the limitation in the INDIRECT function.

    The VBA is very simple:

    New_Ref = Sheets("Wells").Range("K6")

    ActiveWorkbook.Names("MyWorkbook").RefersTo = "=" & New_Ref

    The only trick is to be sure to include "=" in the name.

    Names have a huge number of uses once you spot this. I have used this to get data from a closed file on a remote sharepoint site without any difficulty - I assume sharepoint deals with all the permissions.

    0 讨论(0)
  • 2021-01-06 02:57

    I think what you what to do is to find the specific record in the specific file (date named). You may do it by a simple VBA code.

    Suppose you are going to search for a record# say REC001 in A1, date file 12.10.12 at cell C1, and have the result to be display at cell A7

    On the worksheet you want to enter input and get output, rightclick the sheet tab and select 'View code' and paste the following code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("C1")) Is Nothing Then Exit Sub
    Range("A7").Formula = "=INDEX('C:\TEMP\[workbook" & Range("C5").Value & ".xls]SHEET1'!$B$1:$B$5, MATCH(" & Range("A1").Value & ", 'C:\TEMP\[workbook" & Range("C5").Value & ".xls]SHEET1'!$A$1:$A$5, 0))"
    End Sub
    

    Then every time you edit C1, the formula will be updated.

    Actually I don't think you should use INDEX function in your case. It is more simple to use a VLOOKUP. E.g.:

    Range("A8").Formula = "=vlookup(" & Range("A1").Value & ",'C:\TEMP\[workbook" & Range("C5").Value & ".xls]SHEET1'!$A$1:$B$5,2,false)"
    

    You will have to note on a few points: 1. you paste the code on the Sheet1 object (or the sheet name) but not to insert a new module 2. your path and filename for the target file is correct, including the .xls and .xlsx 3. your original file only cover to $B$5 4. on VBA, recommend you to save the file as .xlsm format

    0 讨论(0)
  • 2021-01-06 03:01

    From Frank Kabel's 2004 post at Dicks Blog you could

    1. Use Laurent Longre has developed the free add-in MOREFUNC.XLL which includes the function INDIRECT.EXT
    2. Use SQL.REQUEST as described here *does not appear to be supported anymore and I am not clear if this could handle your INDEX\MATCH request
    3. Use Harlan Grove’s PULL function

    In addition you could:

    1. Create a "dirty link" directly via code that enters a formula referring to the workbook you need
    2. For pulling values - but not for working with ranges - you could use Walkenbach's ExecuteExcel4Macro XLM method
    0 讨论(0)
提交回复
热议问题