LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default need macro for renaming bunch of excel files

The Filesearch method is less code, but it is slower and it relies on a
reference to the
Filesearch library, so I prefer my method.
To avoid an error and make the added number one higher I made a small
adaptation:


Sub RenameFiles(strFolder As String, _
strExtension As String, _
strFileExclude As String, _
strNewName As String, _
lMaxFiles As Long, _
Optional bKillOld As Boolean = False)

Dim arr
Dim i As Long
Dim lCounter As Long
Dim lCounterAdd As Long

On Error GoTo ERROROUT

arr = GetFilesInFolder(strFolder & "\*." & strExtension)

For i = 1 To UBound(arr)
If arr(i) < strFileExclude And _
lCounter < lMaxFiles Then

Do While Len(Dir(strFolder & "\" & _
strNewName & lCounter + 1 + lCounterAdd & _
"." & strExtension)) 0
lCounterAdd = lCounterAdd + 1
Loop

FileCopy strFolder & "\" & arr(i), _
strFolder & "\" & strNewName & lCounter + 1 +
lCounterAdd & "." & strExtension
lCounter = lCounter + 1

If bKillOld Then
Kill strFolder & "\" & arr(i)
End If

End If
Next

Exit Sub
ERROROUT:

MsgBox "no files found", , "rename files in folder"
On Error GoTo 0

End Sub


RBS


"neowok" wrote in
message ...

thanks, I have found a shorter solution which is

Sub renfiles()
Dim I As Long
Dim NoFiles As Long
Dim strOldName As String
Dim strNewName As String

With Application.FileSearch
NewSearch
LookIn = ThisWorkbook.Path
FileType = msoFileTypeExcelWorkbooks
Execute
NoFiles = IIf(.FoundFiles.Count 40, 40, .FoundFiles.Count -
1)

For I = 0 To NoFiles
If .FoundFiles(I + 1) < ThisWorkbook.FullName Then
strOldName = .FoundFiles(I + 1)
strNewName = ThisWorkbook.Path & "\EX" & Format(I + 1,
"0") & ".xls"
Name strOldName As strNewName
End If
Next I
End With

End Sub

the only problem I have with this one at the moment is if an ex1 etc
file already exists when it tries to rename a file to ex1 then it
causes a runtime error, when it should rename it to ex2 instead if ex1
already exists.

thanks


--
neowok
------------------------------------------------------------------------
neowok's Profile:
http://www.excelforum.com/member.php...fo&userid=5940
View this thread: http://www.excelforum.com/showthread...hreadid=394886


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Renaming excel files in a folder Rhett C[_2_] Excel Discussion (Misc queries) 1 January 29th 10 05:23 AM
Excel Renaming Files When Opening Using File Convertor William McCann Excel Discussion (Misc queries) 0 November 25th 09 03:41 PM
How to print a bunch of files form the list (excel)? Tojkior Excel Discussion (Misc queries) 0 June 9th 08 01:55 PM
Renaming Files w/Excel Confused VB Person Excel Discussion (Misc queries) 4 March 21st 08 09:26 AM
Batch renaming of many worksheets in Excel files? Lumen S Excel Discussion (Misc queries) 2 August 17th 06 09:48 PM


All times are GMT +1. The time now is 04:12 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"