View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default can I remove part of a file name (LO) from a group of excel files

IF the "LO" you want to omit is the first occurrence of the letters "LO" in
the file name, AND ALL the files are in the same folder AND you want to
possibly rename all the files in the folder, you can use code like the
following. Change the lines marked with '<<<<< to the appropriate values.


Sub RenameFiles()

Dim FName As String
Dim RenameCount As Long
Dim ErrCount As Long
Dim RenameStr As String
Dim ErrStr As String
Dim ReplaceName As String

ChDrive "C:" '<<<<< CHANGE
ChDir "C:\Test" '<<<<< CHANGE

FName = Dir("*.xls")
On Error Resume Next
Do Until FName = vbNullString
ReplaceName = Replace(expression:=FName, Find:="LO", _
Replace:=vbNullString, Count:=1, compa=vbTextCompare)
Err.Clear
Name FName As ReplaceName
If Err.Number = 0 Then
RenameStr = RenameStr & vbCrLf & ReplaceName
RenameCount = RenameCount + 1
Else
ErrStr = ErrStr & vbCrLf & FName
ErrCount = ErrCount + 1
End If
FName = Dir()
Loop

MsgBox "Files Renamed: " & CStr(RenameCount) & vbCrLf & RenameStr
If ErrCount 0 Then
MsgBox "Error renaming files: " & CStr(ErrCount) & vbCrLf & ErrStr
End If

End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"Geekedersch" wrote in message
...
my output from testing always has (LO) in the file names. no parenthesis.
To use the files we have to delete the LO from each file name. Is there a
way to remove the LO from the whole group at one time?
Thanks for any help.
Elaine