'VBA打开文件选择框、取得文件全路径与文件名
Sub selectExcelfile()
Dim fileNameObj As Variant
Dim aFile As Variant '数组,提取文件名fileName时使用
'打开文件对话框返回的文件名,是一个全路径文件名,其值也可能是False,因此类型为Variant
Dim fullName As String
Dim fileName As String '从FileName中提取的路径名
Dim i As Integer
fileNameObj = Application.GetOpenFilename("Excel 文件 (*.xls),*.xls")
'调用Windows打开文件对话框
If fileNameObj <> False Then '如果未按“取消”键
aFile = Split(fileNameObj, "\")
fileName = aFile(UBound(aFile)) '数组的最后一个元素为文件名
fullName = aFile(0)
For i = 1 To UBound(aFile) '循环合成全路径
fullName = fullName & "\" & aFile(i)
Next
Else
MsgBox "请选择文件"
End
End If
'得到Excel全路径
allExcelFullPath = fullName
'得到Excel文件名
workbookName = fileName
End Sub
【VBA】VBA打开文件选择框、取得文件全路径与文件名
最新推荐文章于 2025-04-15 08:57:16 发布