VBA查找并替换部分文件路径 [英] VBA Find and replace part of file path

查看:255
本文介绍了VBA查找并替换部分文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我难以编写 VBA 来查找和替换文件路径的一部分。我需要隔离文件名,而不需要文件扩展名(即更换包括最后一个 \ .flac 下面的例子)

I'm having difficulty writing VBA to find and replace part of a file path. I need to isolate the filename without the file extension (i.e. replace everything including and to the left of the last \ and the .flac for the example below)

类型查找和替换涉及文件路径,如下所示 -

The type find and replace involves file paths that look like --

C:\Users\myname\Desktop\PhoneCallFolder1\123456789_20140101120101.flac

C:\Users\othername\Desktop\PhoneCallFolder2\\
$ b

C:\Users\myname\Desktop\PhoneCallFolder1\123456789_20140101120101.flac
C:\Users\othername\Desktop\PhoneCallFolder2\123456789_19990101120101.flac

,结果应该如下 -

and the result should look like --


20140101120101

19990101120101

20140101120101
19990101120101

感谢您的帮助。我现在的代码如下:

Thanks for your help. My existing code is below:

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


推荐答案

您可以使用通配符替换任何路径,如此

You can use a wild card to replace any path, like this

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

这篇关于VBA查找并替换部分文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆