我每天/每周下载报告但是在下载系统时自动生成文件名,最后有一个日期,尽管基本文件名是相同的。即ANAPOS - 20141001.我正在使用一个简单的打开命令(Workbooks.OpenText文件名:=“C:\ Users \ 903270 \ Documents \ Excel \ ANAPOS.txt”)来做其他一些事情,但在这之前我需要重命名我可以运行之前将文件发送到ANAPOS.txt。 是否有任何代码允许我的宏搜索ANAPOS而不包含最后的所有其他信息? 任何帮助表示赞赏。
答案 0 :(得分:0)
将filePath
设置为您要搜索的位置
Sub getANAPOS()
Dim Filter As String, filePath As String
filePath = "C:\Data\VBA\SO\"
Filter = "ANAPOS files (*.txt), filepath & ANAPOS*.txt"
ANAPOSSelectedFile = Application.GetOpenFilename(Filter)
End Sub
按OP进行澄清后的编辑
坚持使用相同的主题,这应该给你一些工作范围。它本质上是自动的#39;除非已存在,否则将原位重命名所选文件。感谢@Gary的学生提出的解析GetOpenFileName结果的好主意here。
Sub renameANAPOS()
Dim Filter As String, filePath As String, newName As String
'filter txt file names containing 'ANAPOS'
Filter = "ANAPOS files (*.txt), filepath & ANAPOS*.txt"
'the 'rename' name
newfName = "ANAPOS"
'navigate to original ANAPOS file and location details
ANAPOSSelectedFile = Application.GetOpenFilename(Filter)
'parse selected file details
fullArr = Split(ANAPOSSelectedFile, "\")
detArr = Split(fullArr(UBound(fullArr)), ".")
fullArr(UBound(fullArr)) = ""
fPath = Join(fullArr, "\")
fName = detArr(0)
fExt = detArr(1)
'rename file in not already exixts
If Len(Dir(fPath & newfName & "." & fExt)) > 0 Then
MsgBox newfName & "." & fExt & " already exists in this folder."
Exit Sub
Else
Name ANAPOSSelectedFile As fPath & newfName & "." & fExt
End If
End Sub