VBA裁剪网站/位置地址

问题描述 投票:0回答:1

我正在尝试使用一些用于剪切文件位置的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返回。文件名会有所不同,因此我正在尝试从右侧切换到第一个反斜杠。

vba excel-vba excel
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.