I am trying to open a workbook, do some stuff, and close the workbook when called from a cell - i.e. using a Public Function
.
When I run the code in a sub
routine, it works fine. But for some reason when I run it from a cell, passing all parameters exactly the same, it fails to open the workbook.
Is this something that is known to be the case?
Code for sub (works):
Sub testing()
Dim sName As String
Dim tName As String
sName = [Config.Folder.Core.GS].Value & "\" & [Config.FileName21].Value
Dim wb As Workbook
Debug.Print sName
Set wb = Workbooks.Open(sName)
' opens workbook fine!!!
End Sub
Code for public function (does not work):
Public Function NewCsvWithoutRowsFromXLS(sSourceName As String, sTargetName As String, rowExclusionsList As String, Optional iRowsToDeleteFromTop As Integer, Optional iCharsFromLeft As Long) As String
Application.Volatile True
On Error GoTo EH
Dim wb As Workbook
' open wb
Set wb = Workbooks.Open(sSourceName)
' fails to set wb object!!!
End Function
Fyi, file names are identical when opening - have double, and triple checked!