仅从完整路径中获取文件名

Grab File Name Only From Full Path

在 VBA 中,我如何才能仅从下面的完整路径中获取文件名?当在下面的语法中使用时,它会将完整的文件路径(没有 .xlsx)导入为 table 名称,这不是我想要的。

Public Function importExcelSheets() As Long
Dim Directory As String, TableName As String, strDir As String, strFile As String, I As Long
Directory = "C:\Test"
On Error Resume Next
 I = 0
 If Left(Directory, 1) <> "\" Then
     strDir = Directory & "\"
 Else
     strDir = Directory
 End If
 strFile = Dir(strDir & "*.XLSX")
 TableName = strDir & strFile
 While strFile <> ""
     I = I + 1
     strFile = strDir & strFile
     DoCmd.TransferSpreadsheet acImport, , TableName, strFile, True
     strFile = Dir()
 Wend
 importExcelSheets = I
End Function

您在此处指定:TableName = strDir & strFile 表名是完整路径。只需删除目录组件:TableName = strFile

您只需删除最后 5 个字符即可删除 .xslxTableName = Left(TableName, Len(TableName) -5 )