VBA查找并replace部分文件path
我很难编写VBA
来查找和replace部分文件path。 我需要隔离没有文件扩展名的文件名(即replace所有包括和最后\
的左侧和下面的例子.flac
)
types查找和replace涉及文件path,看起来像 –
C:\用户\ MYNAME \桌面\ PhoneCallFolder1 \ 123456789_20140101120101.flac
C:\用户\中文别名:\桌面\ PhoneCallFolder2 \ 123456789_19990101120101.flac
结果应该看起来像 –
20140101120101
19990101120101
谢谢你的帮助。 我现有的代码如下:
Columns("A:A").Select Selection.Replace What:= _ "C:\Users\myname\Desktop\PhoneCallFolder1\" _ , Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:= _ False, SearchFormat:=False, ReplaceFormat:=False Selection.Replace What:= _ ".flac" _ , Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:= _ False, SearchFormat:=False, ReplaceFormat:=False
您可以使用通配符replace任何path,像这样
With Columns("A:A") .Replace What:= _ "*\", _ Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, _ MatchCase:= False, SearchFormat:=False, ReplaceFormat:=False .Replace What:= _ ".*", _ Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, _ MatchCase:= False, SearchFormat:=False, ReplaceFormat:=False End With
如果您需要一次处理一个replace件,则可以使用如下几个function:
Function FileNameParse(strFile as String) ' Return a file name from a full path FileNameParse = Left(strFile, InStrRev(strFile, "\") - 1) End Function Function FilePathParse(strFile as String) ' Return a directory from a full path FilePathParse = Right(strFile, Len(strFile) - InStrRev(strFile, "\")) End Sub
最简单的方法是,总是得到一个14个字符的string,从最后开始定位5个字符。 你可以用一个看起来像这样的公式来做到这一点。
对于单元格A1:
=LEFT(RIGHT(A1, 19), 14)
然后将这个公式一路向下。
我会详细说明一个vba的方法。
VBA:
lastRow = Cells(Rows.Count, 1).End(xlUp).Row 'this only finds the last row For x = 1 To lastRow 'This loops through all the rows Cells(x, 1) = Left(Right(Cells(x, 1), 19), 14) Next