Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

VBA function refuses to return a string

Function getExcelFolderPath2() As String

    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    
    Dim fullPath As String
    
    fullPath = fso.GetAbsolutePathName(ThisWorkbook.Name)
    
    fullPath = Left(fullPath, Len(fullPath) - InStr(1, StrReverse(fullPath), "\")) & "\"
    
    getExcelFolderPath2 = fullPath

End Function

Even though fullPath gets a string with relevant contents, getExcelFolderPath2 ends up being empty. This is from a break on that last line, just to be clear:

enter image description here

There must be something really simple that I’m missing, but I can’t see it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I expected to have getExcelFolderPath2 be equal to fullPath.

>Solution :

This is more a comment than an answer, but too long:

I guess the function works correctly. It works for me (and at least for user Dominique).

Could it be that you set the breakpoint on the statement getExcelFolderPath2 = fullPath and then look to the Locals Window? When the runtime hits that statement and breaks, the statement itself is not executed.

enter image description here

But if you step to the end of the function, the function value is set

enter image description here

Call the function from the immediate window to check:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading