VBA函数拒绝返回一个字符串。

huangapple go评论72阅读模式
英文:

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:

VBA函数拒绝返回一个字符串。

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.

VBA函数拒绝返回一个字符串。

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

VBA函数拒绝返回一个字符串。

Call the function from the immediate window to check:

VBA函数拒绝返回一个字符串。

答案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)

huangapple
  • 本文由 发表于 2023年4月13日 15:19:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定