2015-06-05 72 views
-2

我有一个.xls文件,并且想要使用VBA宏从单元格中提取文件名和扩展名。例如:在Excel中提取文件名和扩展名

c:\Documents\One.psd 

我需要在单独的变量单独的变量和PSD的名称。

问候

萨蒂什

+0

你有没有看着[本所以问题](http://stackoverflow.com/questions/12354592/excel-vba-regular-expression-get-file-name )? –

回答

0

考虑:

Sub qwerty() 
    s = Range("A1").Value 
    ary = Split(s, "\") 
    bry = Split(ary(UBound(ary)), ".") 
    fname = bry(0) 
    ext = bry(1) 
    MsgBox fname & vbCrLf & ext 
End Sub 
0

得到的文件路径部分最简单的方法是使用脚本运行的FileSystemObject的:

Dim fso As New Scripting.FileSystemObject 
Dim filepath As String 

filepath = "C:\Somewhere\foo.bar" 

'Outputs "C:\Somewhere" 
Debug.Print fso.GetParentFolderName(filepath) 
'Outputs "foo.bar" 
Debug.Print fso.GetFileName(filepath) 
'Outputs "foo" 
Debug.Print fso.GetBaseName(filepath) 
'Outputs "bar" 
Debug.Print fso.GetExtensionName(filepath)