简体   繁体   English

在另一个 Excel 工作簿 VBA 中修改嵌入的 Excel 工作簿

[英]Modify embedded Excel workbook in another Excel workbook VBA

I have an Excel workbook in which I have embedded another Excel workbook.我有一个 Excel 工作簿,其中嵌入了另一个 Excel 工作簿。 I am able to open it with VBA, but I have no idea how to refer and edit some cells in embedded workbook.我可以用 VBA 打开它,但我不知道如何在嵌入式工作簿中引用和编辑一些单元格。 Any idea how to do that?知道怎么做吗? Thanks a lot in advance.提前非常感谢。

Sub openembeddedXL2()

Sheets("sheet1").OLEObjects("SalesFile").Activate

End Sub

As long as a workbook is open you can directly refer to it by its name.只要工作簿是打开的,您就可以直接通过其名称来引用它。

Workbooks("workbook name") etc. Workbooks("workbook name")

Since you open the workbook with Sheets("sheet1").OLEObjects("SalesFile").Activate the workbook related to the object will then be opened as a file called "Worksheet in your current workbook ".由于您使用Sheets("sheet1").OLEObjects("SalesFile").Activate打开工作簿,因此与 object 相关的工作簿将作为名为“当前工作簿中的工作表”的文件打开。 You can therefore use:因此,您可以使用:

Dim wb as workbook
Sheets("sheet1").OLEObjects("SalesFile").Activate
set wb = Workbooks("Worksheet in " & ThisWorkbook.Name)
Thisworkbook.sheets("Sheet1").Range("A1").value = wb.sheets("Sheet1").range("A1").Value 'etc. etc.
wb.Close

Thisworkbook is a handy tool here, as it will always refer to the workbook the macro is in, despite which workbook is currently active. Thisworkbook在这里是一个方便的工具,因为它始终会引用宏所在的工作簿,无论哪个工作簿当前处于活动状态。

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

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