如何在VBA中将打开的Excel文件设置为Workbook对象

时间:2018-05-15 14:48:21

标签: excel vba access-vba

我需要设置一个已在计算机上作为工作簿对象打开的Excel文件。

Dim wb As Workbook
Dim sh As Worksheet

Set wb = *the open excel file*
Set sh = wb.Worksheets("FlatFile-FichierPlat")

3 个答案:

答案 0 :(得分:3)

Set wb = Workbooks("yourWorkbookName")

答案 1 :(得分:1)

您可以使用GetObject获取对运行它的Excel应用程序的引用,然后迭代工作簿集合:

Dim wb As Workbook
Dim sh As Worksheet

Set wb = GetObject( ,"Excel.Application").ActiveWorkbook
Set sh = wb.Worksheets("FlatFile-FichierPlat")

有关详细信息,请参阅MSDN

答案 2 :(得分:1)

如果您的代码正在打开文件,那么您已经拥有了对象引用:

containerRegistry.RegisterSingleton<ILoggerFacade, EmptyLogger>();

如果用户可以在任意时间点打开文件,那么您需要处理Dim book As Workbook Set book = Application.Workbooks.Open(path) 'reference is returned by the Open function 事件 - 您可以通过在{{模块范围内声明Application.WorkbookOpen对象变量来执行此操作。 1}},并在Private WithEvents {name} As Excel.Application打开时设置对ThisWorkbook的引用,即在Application处理程序中:

ThisWorkbook