英文:
VBA function refuses to return a string
问题
功能 getExcelFolderPath2() 作为 字符串
Dim fso 作为 FileSystemObject
设置 fso 为 New FileSystemObject
Dim fullPath 作为 字符串
fullPath = fso.GetAbsolutePathName(ThisWorkbook.Name)
fullPath = Left(fullPath, Len(fullPath) - InStr(1, StrReverse(fullPath), "\")) & "\"
getExcelFolderPath2 = fullPath
结束 功能
英文:
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:
There must be something really simple that I'm missing, but I can't see it.
I expected to have getExcelFolderPath2 be equal to fullPath.
答案1
得分: 2
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.
But if you step to the end of the function, the function value is set
Call the function from the immediate window to check.
英文:
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.
But if you step to the end of the function, the function value is set
Call the function from the immediate window to check:
答案2
得分: 0
这是工作正常的。
你如何调用那个函数?我只是简单地在单元格中放置=getExcelFolderPath2()
(比如“B3”)。
哦,你是否在VBA引用列表中添加了Microsoft Scripting Runtime?(VBA编辑器,"工具"菜单,"引用"项)
英文:
For me, this is working fine.
How do you call that function? I have simplye put =getExcelFolderPath2()
inside a cell (like "B3").
Oh, did you add the Microsoft Scripting Runtime in your list of VBA references? (VBA editor, "Tools" menu, "References" item)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论