如何从VBA中的路径提取文件名 [英] How to extract a file name from its path in vba

查看:70
本文介绍了如何从VBA中的路径提取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写代码,其中正在打开用于提取数据的文件.我当前正在使用以下代码;我想从路径中提取文件名并将其存储在特定范围内.这段代码:

I am writing a code, where I am opening a file for extracting data. I am currently using following code; I want to extract the file name from the path and store it in a particular range. This the code:

FilePath = Application.GetOpenFilename("Excel Files (*.xlsx), *.xls")
If FilePath <> False Then
Range("D6").Value = FilePath
file = Range("D6").Value
Range("D6").Clear
End If

推荐答案

您可以按照以下步骤进行操作:

You can do it like below:

FilePath = Application.GetOpenFilename("Excel Files (*.xlsm), *.xlsm")

If FilePath <> False Then
    Dim fso As Object
    Dim objFile As Object

    Set fso = VBA.CreateObject("Scripting.FileSystemObject")
    Set objFile = fso.GetFile(FilePath)

    If Not objFile Is Nothing Then
        FileName = objFile.Name
    End If

End If

这篇关于如何从VBA中的路径提取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆