Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Renaming Files Problem Continued

I tried the program below but I wasn't able to make it rename the files and
it doesn't loop. Could you please test it, and let me know if there is
anything that should be changed?

My problem was:
500 excel files are in a folder. I need to rename each one as:
"lbif08" & first 5 digits of Cell A1 of each file &".xls"


Option Explicit
Sub RenameFiles()
Dim OldName() As String
Dim NewName() As String
Dim bk As Workbook
Dim sPath As String, sName As String
Dim i As Long, j As Long
' change to reflect your directory
sPath = "C:\Myfolder\Myfiles\"
sName = Dir(sPath & "*.xls")
ReDim OldName(1 To 1)
ReDim NewName(1 To 1)
i = 1
Do While sName < ""
OldName(i) = sName
Set bk = Workbooks.Open(sPath & sName)
NewName(i) = "lbif08" & Left(bk.Worksheets( _
1).Range("A1").Text,5) & ".xls"
bk.Close SaveChanges:=False
i = i + 1
ReDim Preserve OldName(1 To i)
ReDim Preserve NewName(1 To i)
Loop
For j = 1 To i - 1
Name sPath & OldName(i) As sPath & NewName(i)
Next
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 747
Default Renaming Files Problem Continued

Test the following adjusted code on a separate folder with a limited number
of copied files. Minimal testing:-

Sub RenameFiles()
Dim OldName() As String
Dim NewName() As String
Dim bk As Workbook
Dim sPath As String, sName As String
Dim i As Long, j As Long
' change to reflect your directory
sPath = "C:\Myfolder\Myfiles\"
sName = Dir(sPath & "*.xls")
ReDim OldName(1 To 1)
ReDim NewName(1 To 1)
i = 1
Do
OldName(i) = sName
Set bk = Workbooks.Open(sPath & sName)
NewName(i) = "lbif08" & Left(bk.Worksheets( _
1).Range("A1").Text, 5) & ".xls"
bk.Close SaveChanges:=False
i = i + 1
sName = Dir()
ReDim Preserve OldName(1 To i)
ReDim Preserve NewName(1 To i)
Loop While sName < ""
For j = 1 To i - 1
Name sPath & OldName(j) As sPath & NewName(j)
Next
End Sub

Regards,
Greg

"Filo" wrote:

I tried the program below but I wasn't able to make it rename the files and
it doesn't loop. Could you please test it, and let me know if there is
anything that should be changed?

My problem was:
500 excel files are in a folder. I need to rename each one as:
"lbif08" & first 5 digits of Cell A1 of each file &".xls"


Option Explicit
Sub RenameFiles()
Dim OldName() As String
Dim NewName() As String
Dim bk As Workbook
Dim sPath As String, sName As String
Dim i As Long, j As Long
' change to reflect your directory
sPath = "C:\Myfolder\Myfiles\"
sName = Dir(sPath & "*.xls")
ReDim OldName(1 To 1)
ReDim NewName(1 To 1)
i = 1
Do While sName < ""
OldName(i) = sName
Set bk = Workbooks.Open(sPath & sName)
NewName(i) = "lbif08" & Left(bk.Worksheets( _
1).Range("A1").Text,5) & ".xls"
bk.Close SaveChanges:=False
i = i + 1
ReDim Preserve OldName(1 To i)
ReDim Preserve NewName(1 To i)
Loop
For j = 1 To i - 1
Name sPath & OldName(i) As sPath & NewName(i)
Next
End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 747
Default Renaming Files Problem Continued

Thanks for the feedback. Tom did 98% of the work. If he didn't you probably
wouldn't have received an answer.

Greg

"Filo" wrote:

Greg, You made my day. Thank you!!!!

"Greg Wilson" wrote:

Test the following adjusted code on a separate folder with a limited number
of copied files. Minimal testing:-

Sub RenameFiles()
Dim OldName() As String
Dim NewName() As String
Dim bk As Workbook
Dim sPath As String, sName As String
Dim i As Long, j As Long
' change to reflect your directory
sPath = "C:\Myfolder\Myfiles\"
sName = Dir(sPath & "*.xls")
ReDim OldName(1 To 1)
ReDim NewName(1 To 1)
i = 1
Do
OldName(i) = sName
Set bk = Workbooks.Open(sPath & sName)
NewName(i) = "lbif08" & Left(bk.Worksheets( _
1).Range("A1").Text, 5) & ".xls"
bk.Close SaveChanges:=False
i = i + 1
sName = Dir()
ReDim Preserve OldName(1 To i)
ReDim Preserve NewName(1 To i)
Loop While sName < ""
For j = 1 To i - 1
Name sPath & OldName(j) As sPath & NewName(j)
Next
End Sub

Regards,
Greg

"Filo" wrote:

I tried the program below but I wasn't able to make it rename the files and
it doesn't loop. Could you please test it, and let me know if there is
anything that should be changed?

My problem was:
500 excel files are in a folder. I need to rename each one as:
"lbif08" & first 5 digits of Cell A1 of each file &".xls"


Option Explicit
Sub RenameFiles()
Dim OldName() As String
Dim NewName() As String
Dim bk As Workbook
Dim sPath As String, sName As String
Dim i As Long, j As Long
' change to reflect your directory
sPath = "C:\Myfolder\Myfiles\"
sName = Dir(sPath & "*.xls")
ReDim OldName(1 To 1)
ReDim NewName(1 To 1)
i = 1
Do While sName < ""
OldName(i) = sName
Set bk = Workbooks.Open(sPath & sName)
NewName(i) = "lbif08" & Left(bk.Worksheets( _
1).Range("A1").Text,5) & ".xls"
bk.Close SaveChanges:=False
i = i + 1
ReDim Preserve OldName(1 To i)
ReDim Preserve NewName(1 To i)
Loop
For j = 1 To i - 1
Name sPath & OldName(i) As sPath & NewName(i)
Next
End Sub


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Renaming Files Problem Continued

Greg, You made my day. Thank you!!!!

"Greg Wilson" wrote:

Test the following adjusted code on a separate folder with a limited number
of copied files. Minimal testing:-

Sub RenameFiles()
Dim OldName() As String
Dim NewName() As String
Dim bk As Workbook
Dim sPath As String, sName As String
Dim i As Long, j As Long
' change to reflect your directory
sPath = "C:\Myfolder\Myfiles\"
sName = Dir(sPath & "*.xls")
ReDim OldName(1 To 1)
ReDim NewName(1 To 1)
i = 1
Do
OldName(i) = sName
Set bk = Workbooks.Open(sPath & sName)
NewName(i) = "lbif08" & Left(bk.Worksheets( _
1).Range("A1").Text, 5) & ".xls"
bk.Close SaveChanges:=False
i = i + 1
sName = Dir()
ReDim Preserve OldName(1 To i)
ReDim Preserve NewName(1 To i)
Loop While sName < ""
For j = 1 To i - 1
Name sPath & OldName(j) As sPath & NewName(j)
Next
End Sub

Regards,
Greg

"Filo" wrote:

I tried the program below but I wasn't able to make it rename the files and
it doesn't loop. Could you please test it, and let me know if there is
anything that should be changed?

My problem was:
500 excel files are in a folder. I need to rename each one as:
"lbif08" & first 5 digits of Cell A1 of each file &".xls"


Option Explicit
Sub RenameFiles()
Dim OldName() As String
Dim NewName() As String
Dim bk As Workbook
Dim sPath As String, sName As String
Dim i As Long, j As Long
' change to reflect your directory
sPath = "C:\Myfolder\Myfiles\"
sName = Dir(sPath & "*.xls")
ReDim OldName(1 To 1)
ReDim NewName(1 To 1)
i = 1
Do While sName < ""
OldName(i) = sName
Set bk = Workbooks.Open(sPath & sName)
NewName(i) = "lbif08" & Left(bk.Worksheets( _
1).Range("A1").Text,5) & ".xls"
bk.Close SaveChanges:=False
i = i + 1
ReDim Preserve OldName(1 To i)
ReDim Preserve NewName(1 To i)
Loop
For j = 1 To i - 1
Name sPath & OldName(i) As sPath & NewName(i)
Next
End Sub


Reply
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 Files Gordon[_2_] Excel Programming 1 August 1st 06 08:52 AM
Range Problem #2 (continued) Steven Drenker[_4_] Excel Programming 2 January 23rd 06 07:16 PM
Renaming Zip files GEORGIA Excel Programming 6 September 15th 05 01:19 PM
Renaming Files mudraker[_304_] Excel Programming 2 August 13th 04 12:04 AM
Renaming Files Spammastergrand Excel Programming 5 September 19th 03 10:43 PM


All times are GMT +1. The time now is 09:11 PM.

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

About Us

"It's about Microsoft Excel"