How to reference a workbook from another excel instance

回眸只為那壹抹淺笑 提交于 2020-02-28 19:02:26

问题


I believe my problem is rather simple: I have a workbook and I'm using it to get some data from another software (SAP). When I export the data from the software, it automatically opens a .xlsx file, then what I'd need to do is copy some of the data from this file, paste on my original workbook, and then close the file. The section of my code which is giving me an error is this one:

fileName = "temp1.xlsx"
Set wbBasis = Workbooks(fileName)

This happens because the "temp1.xlsx" file that was opened by the SAP software is in another instance of excel, so my code isn't able to find it.

What I need to know is basically this. How to properly reference this "temp1.xlsx" workbook on my original code so that I'm able to edit it, copy stuff from it, close it, etc.

I've found some similar topics to my problem, like the two I'm listing down here, but couldn't adapt the solutions to my situation, and that's why I'm posting this new one.

Having multiple excel instances launched, how can I get the Application Object for all of them?

Finding a workbook in one of multiple Excel instances

Thank you in advance.


回答1:


You don't need multiple instances of Excel, and you don't need the Excel file to be open in order to get information from it, either with VBA, or even with regular "linked formulas".

For example:

=SUM([C:\yourPath\Filename.xlsx]SheetName!A1:C25)

...returns the sum of cells A1:C25 on a worksheet named Sheetname in an Excel file located at C:\yourPath\Filename.xlsx.

Or you can import all the data directly with Get External Data.


See:

  • MSDN : Create an external reference (link) to a cell range in another workbook
  • MSDN : Connect to another workbook

...for more information and examples, search Google for "get data from another Excel workbook".



来源:https://stackoverflow.com/questions/51366121/how-to-reference-a-workbook-from-another-excel-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!