简体   繁体   English

在不复制工作表的情况下从另一个工作簿引用 Excel 工作表

[英]Reference an excel sheet from another workbook without copying the sheet

Im wondering if it's possible to reference an excel sheet from another work book without making a copy of that sheet?我想知道是否可以在不复制该工作表的情况下从另一本工作簿中引用 Excel 工作表?

The situation : I have some very large worksheets filled with various data, but i don't want to keep a copy of them open in my workbooks because while each workbook uses the same data source, they're slightly different.情况:我有一些非常大的工作表,里面装满了各种数据,但我不想在我的工作簿中打开它们的副本,因为虽然每个工作簿使用相同的数据源,但它们略有不同。

I have a vba routine that takes this data and creates input files for other codes, vba expects this data to be available on the defined sheet names.我有一个 vba 例程,它接受这些数据并为其他代码创建输入文件,vba 希望这些数据在定义的工作表名称上可用。

Is it possible to make either excel or vba to know that when i request worksheet("Example_1") it instead knows that i mean example_1 from a different workbook?是否可以让 excel 或 vba 知道当我请求工作表(“Example_1”)时,它知道我的意思是来自不同工作簿的 example_1?

Thanks谢谢

Yes, it is possible.对的,这是可能的。

You need to add those lines to your code:您需要将这些行添加到您的代码中:

Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet

Set wkb = Excel.Workbooks("name_of_workbook.xlsx")
Set wks = wkb.Worksheets("Example_1")

Now, every time you want to refer to a range from this other workbook, you need to add wks.现在,每次您想要引用其他工作簿中的范围时,您都需要添加wks. before, ie:之前,即:

'Printing value in cell.
wks.Range("A1") = "x"

'Selecting range.
call wks.Range(wks.Cells(1,1), wks.Cells(2,2)).Select
=SUM('C:\test\[test.xlsx]sheet_name'!A1:A25)

is an example of a formula which references sheet sheet_name in workbook C:\\test\\text.xlsx .是引用工作簿C:\\test\\text.xlsx中工作表sheet_name的公式示例。

Note that when the other workbook is opened, the formula automatically changes to请注意,打开另一个工作簿时,公式会自动更改为

=SUM([test.xlsx]sheet_name!A1:A25)

and then when it is closed, the formula will change back.然后当它关闭时,公式会变回来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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