1

I have written a macro that looks for the word "active" rather than "activ" but some of the filenames in my folder may contain the word "activ". I want to write a macro to search for these and rename them as "active" so the rest of my macro works. i know bits and pieces but can't seem to put a decent code together.

If FileName = "*activ*" then 

'also the file name is not just "activ" it maybe "activcn". 
'am i doing this right by putting the **?

FileName = replace(FileName, "activ", "active")

Else 

End if

and I need to put a loop in so it runs through the list of files and searches all filenames.

Actually I've found a solution:

Dim strFile As String
Dim newPath As String

newPath = "S:\test\"

strFile = Dir(newPath & "*.*")

If strFile = "*activ*" Then

  Do While Len(strFile) > 0
    If InStr(strFile, "activ") > 0 Then
      Name strFolder & strFile As strFolder & replace(strFile, "activ", "active")
    End If
    strFile = Dir()
  Loop
Else

End If

Just need help with:

If strFile = "*activ*" Then

As I said earlier, the filename contains more than just "activ" e.g. "activcn".

4

1 回答 1

0

在我看来,您示例中的逻辑和变量名称有点混乱......这就是我认为您的意思:

Dim strFile As String
Dim strFolder As String

strFolder = "S:\test\"

strFile = Dir(newPath & "*activ*.*") ' no need to look at all files using *.*
Do While Len(strFile) > 0
    If Not strFile Like "*active*" Then 'else "active" gets replaced by "activee"
        Name strFolder & strFile _
            As strFolder & Replace(strFile, "activ", "active")
    End If
    strFile = Dir()
Loop
于 2013-11-13T08:29:13.860 回答