VBA裁剪网站/位置地址

时间:2018-02-28 12:02:02

标签: vba excel-vba excel

我在使用一些用于剪切文件位置的VBA代码时遇到了麻烦。

Option Explicit
Private Sub TESTING()

 Dim strLocation as string
 Dim strFile as string

 strLocation = "TEST\test\TEST\test_file_name.csv"

 strFile = Right$(strLocation, InStr(strLocation, "\") - 1)
 MsgBox strFile

End Sub

我希望实现代码,以便将“test_file_name.csv”作为我的strFile返回。文件名会有所不同,因此我正在尝试从右侧切换到第一个反斜杠。

1 个答案:

答案 0 :(得分:1)

某些方式

1)

Dim strLocation As String
Dim strFile As String

strLocation = "TEST\test\TEST\test_file_name.csv"    
strFile = Right$(strLocation, Len(strLocation) - InStrRev(strLocation, "\"))

2)

Dim strLocation As String
Dim strFile As String  

strLocation = "TEST\test\TEST\test_file_name.csv"
strFile = Split(strLocation, "\")(UBound(Split(strLocation, "\")))

3)

Dim strLocation As String
Dim strFile As String

strLocation = "TEST\test\TEST\test_file_name.csv"
With CreateObject("Scripting.FileSystemObject")
    strFile = .GetFileName(strLocation)
End With