简体   繁体   English

如何在excel vba宏中拆分字符串忽略最后一部分

[英]how to split a string in excel vba macros ignoring last portion

I have a cell which contains path values like: 我有一个包含路径值的单元格,如:

C:/Videos/New/VideoName.mp4

I wanted to take only the path from that cell 我只想从那个单元格中走出路径

So I tried 所以我试过了

elementArray = Split(Cells(4, 2), "/")

So that I can concatenate the strings inside that array everytime 这样我每次都可以连接该数组中的字符串

But the array is still having the full path 但阵列仍然有完整的路径

ie

elementArray[0]=C:/Videos/New/VideoName.mp4

How can ignore the file name and take the path alone? 如何忽略文件名并单独使用路径?

There seems to be some confusion on whether you are using and/or require forward slashes (eg / or Chr(47) ) or back-slashes (eg \\ or Chr(92) ). 对于您是否正在使用和/或需要正斜杠(例如/Chr(47) )或反斜杠(例如\\Chr(92) )似乎存在一些混淆。 Try something like this: 尝试这样的事情:

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

Look to the VBE's Immediate window ( Ctrl + G ) for the results. 查看VBE的立即窗口( Ctrl + G )以获得结果。

To avoid any issue, I would use the GetParentFolderName() method of the FileSystemObject , accessible by adding the reference Microsoft Scripting Runtime . 为了避免任何问题,我将使用FileSystemObjectGetParentFolderName()方法,可以通过添加引用Microsoft Scripting Runtime

Dim fso As New FileSystemObject
parentFolder = fso.GetParentFolderName("your path")

Or in late-binding, as suggested by @SOofWXLS (so you avoid the early-binding of the library into the project): 或者在@SOofWXLS建议的后期绑定中(这样可以避免将库早期绑定到项目中):

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
parentFolder = fso.GetParentFolderName("your path")

I think it's the easiest and most robust way of parsing the parent folder by the full path: you avoid complex string manipulations that might let some "less common" cases apart (eg a Mac's path won't be parsed by the "manual" methods). 我认为这是通过完整路径解析父文件夹的最简单,最强大的方法:避免复杂的字符串操作,这可能会让一些“不常见”的情况分开(例如Mac的路径不会被“手动”方法解析)。

You could use the InStrRev() function: 您可以使用InStrRev()函数:

With Cells(4, 2)
    MsgBox Mid(.Value, 1, InStrRev(.Value, "\") - 1)
End With

Please, read Jeeped comment to the question. 请阅读Jeeped评论的问题。

I would suggest to use StrReverse and InStr functions instead of Split : 我建议使用StrReverseInStr函数而不是Split

Dim s As String, f As String

s = "C:\Videos\New\VideoName.mp4"
f = StrReverse(s)
'FileName
'f = Left(f, InStr(1, f, "\") - 1)
'Path
f = Right(f, InStr(1, f, "\"))
f = StrReverse(f)
MsgBox f

'returns: VideoName.mp4 or path - read above comments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA Excel 根据最后一个逗号分割字符串 - VBA Excel split the string apart based on the last comma vba split(字符串,“\\\\”“Excel - vba split(string, “\\”" Excel 如何在vba excel中用多个分隔符分割字符串? - How to split a string with multiple delimiters in vba excel? 使用宏或字符串命令修改VBA的Excel公式 - modifying excel formulas with macros or vba with string commands 通过分隔符分割字符串的最有效方法,同时使用excel vba忽略所述分隔符的某些实例 - Most efficient way to split a string up by a delimiter while ignoring certain instances of said delimiter using excel vba 如何使用MACROS(VBA)获取excel中最后一个非空单元格的地址 - How to get adress of last non empty cell in a excel using MACROS(VBA) 查找最后一行>合并单元格>复制并粘贴到其中 - Excel VBA宏 - Find last row > merge cells > copy and paste into it - Excel VBA macros 如何在Excel VBA中解析完整字符串并将其拆分为多个字符串? - How to Parsing Full String and split into several String in Excel VBA? 如何在VBA中使用Multichar字符串作为分隔符来拆分字符串(Excel) - how to split string using multichar string as delimiter in vba (excel) 如何仅在一段时间内运行 Excel VBA 宏 - How to Run Excel VBA Macros only for sometime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM