VBA - 函数无法检索文件名。。

VBA - 函数无法检索文件名。。

以下代码不适用于案例 #6“FILE NAME”。出现错误 #NAME?... 我无法弄清楚为什么会出现错误。请帮忙?还有一个单独的问题,为什么我的字母“p”在“ActiveWorkbook.path”中没有大写?

Function GetName(Optional NameType As String) As String

     Application.Volatile

     If Len(NameType) = 0 Then NameType = "OFFICE"
     Select Case UCase(NameType)

          Case Is = "OFFICE", "1"
               GetName = Application.UserName
               Exit Function

          Case Is = "WINDOWS", "2"
               GetName = Environ("UserName")
               Exit Function

          Case Is = "COMPUTER", "3"
               GetName = Environ("ComputerName")
               Exit Function

          Case Is = "PARENT FOLDER", "4"
               Dim splitList As Variant
               Dim FullPath As String
               FullPath = Activeworkbook.path
               splitList = VBA.Split(FullPath, "\")
               GetName = splitList(UBound(splitList, 1))
               Exit Function

          Case Is = "FILE PATH", "5"
               GetName = ThisWorkbook.path
               Exit Function

          Case Is = "FILE NAME", "6"
               Dim strFileName As String
               FullPath = ThisWorkbook.FullName
               strFileName = Mid(FullPath, InStrRev(FullPath, "\") + 1)
               GetName = strFileName
               Exit Function

          Case Else
               GetName = CVErr(xlErrValue)
     End Select

End Function

相关内容