Excel VBA can't open Workbook

后端 未结 7 1691
有刺的猬
有刺的猬 2020-12-11 07:48

First: I\'m using Excel 2007, but the code has to work for Excel 2003 as well.

My problem is the following: I need to access cells in a different workbook, which may

7条回答
  •  甜味超标
    2020-12-11 08:02

    You can use this (similar to what Bruno Leite proposed, but much simpler to write):

    Dim excelApp As New Excel.Application
    excelApp.Visible = False
    Set WB = excelApp.Workbooks.Open(FileName, xlUpdateLinksNever, True)
    

    As UDFs are called repeatedly, you should make sure to do an excelApp.Quit before exiting the function (and a WB.close(False) before) to avoid having countless Excel instances running on your box.

    I spent some thoughts on it and came to the conclusion that you cannot mess around with the workbooks of the current instance of excel while executing a UDF. On the other hand, opening a second instance of excel will do the job without interference.

提交回复
热议问题