我需要设置一个已在计算机上作为工作簿对象打开的Excel文件。
Dim wb As Workbook
Dim sh As Worksheet
Set wb = *the open excel file*
Set sh = wb.Worksheets("FlatFile-FichierPlat")
答案 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