我有一个包含路径值的单元格,例如:
C:/Videos/New/VideoName.mp4
我只想走那个牢房的路
所以我尝试了
elementArray = Split(Cells(4, 2), "/")
这样我就可以每次连接该数组中的字符串
但是数组仍然有完整的路径
即
elementArray[0]=C:/Videos/New/VideoName.mp4
如何忽略文件名而仅使用路径?
最佳答案
在使用和/或要求使用正斜杠(例如/
或Chr(47)
)还是反斜杠(例如\
或Chr(92)
)时似乎有些困惑。尝试这样的事情:
dim sPath as string, sFullPath as string
sFullPath = "C:/Videos/New/VideoName.mp4" ' or C:\Videos\New\VideoName.mp4
if len(sFullPath) > len(replace(sFullPath, Chr(47), vbnullstring)) then
sPath = replace(sFullPath, split(sFullPath, Chr(47))(ubound(split(sFullPath, Chr(47)))), vbnullstring)
debug.print sPath & " with forward slashes"
elseif len(sFullPath) > len(replace(sFullPath, Chr(92), vbnullstring)) then
sPath = replace(sFullPath, split(sFullPath, Chr(92))(ubound(split(sFullPath, Chr(92)))), vbnullstring)
debug.print sPath & " with back-slashes"
else
debug.print "unknown separator"
end if
查找到VBE的立即窗口(Ctrl + G)以获取结果。