Hello there,
I am struggling with a custom function I wrote in the VBA. The problem is that this function create new Excel.Application instance inside it that will open excel workbook from another directory (to extract some values). Although it works fine, it takes quite some seconds to extract these values. I am wondering, is there a method that can drastically speed up this function? The excel has many formula that calls this function, and it takes roughly 1 hr to save the workbook (due to formula auto-calculate and value updating). I am attaching this function below for reference. Thank you very much!
Public Function Ext(Ref As String) As String
Dim excelApp As New Excel.Application
excelApp.Visible = False
Dim book As Workbook
Dim xSht As Worksheet
Dim Val As String
openPos = InStr(Ref, "'")
closePos = InStr(Ref, "]")
bookName = Mid(Ref, openPos + 1, closePos - openPos - 1)
bookName = Replace(bookName, "'", "")
bookName = Replace(bookName, "[", "")
bookName = Replace(bookName, "]", "")
openPos = InStr(Ref, "]")
closePos = InStr(Ref, "!")
sheetName = Mid(Ref, openPos + 1, closePos - openPos - 1)
sheetName = Replace(sheetName, "'", "")
charCt = Len(Ref)
cellName = Right(Ref, charCt - closePos)
Set book = excelApp.Workbooks.Open(bookName)
Set xSht = book.Worksheets(sheetName)
Val = xSht.Range(cellName).Value
book.Close savechanges:=False
excelApp.Quit
Ext = Val
End Function
Bookmarks