Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Help, I have a deadline: Renaming Files in Folder

What is the best way to do the following:

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"

Note: I don't need to keep the original file name.

Please help!!! Thank you!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Help, I have a deadline: Renaming Files in Folder

Use the Name command. Format:
Name oldpathname As newpathname

This assumes none of the New names will conflict with existing names.

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" & bk.Worksheets( _
1).Range("A1").Text & ".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

--
Regards,
Tom Ogilvy



"Filo" wrote in message
...
What is the best way to do the following:

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"

Note: I don't need to keep the original file name.

Please help!!! Thank you!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Help, I have a deadline: Renaming Files in Folder

I missed the first 5 characters. Here is a revision:

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

I assume the first 5 digits are the first 5 characters in the cell.

--
Regards,
Tom Ogilvy

"Tom Ogilvy" wrote in message
...
Use the Name command. Format:
Name oldpathname As newpathname

This assumes none of the New names will conflict with existing names.

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" & bk.Worksheets( _
1).Range("A1").Text & ".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

--
Regards,
Tom Ogilvy



"Filo" wrote in message
...
What is the best way to do the following:

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"

Note: I don't need to keep the original file name.

Please help!!! Thank you!





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,365
Default Help, I have a deadline: Renaming Files in Folder

Filo,
It looks like Tom left out one line of code within the Do While loop. Make
the end of it look like this and see how things go:

ReDim Preserve OldName(1 to i)
Redim Preserve NewName(1 to i)
sName=Dir()
Loop

You said that it didn't do anything? I'm betting it went into a perpetual
loop? And it probably actually did work for one file.

"Filo" wrote:

Tom,

When I tried the program, it didn't work. IT runs but it doesn't perform
anything. Could you check what may be wrong please?

Thank you.

Filo

"Tom Ogilvy" wrote:

I missed the first 5 characters. Here is a revision:

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

I assume the first 5 digits are the first 5 characters in the cell.

--
Regards,
Tom Ogilvy

"Tom Ogilvy" wrote in message
...
Use the Name command. Format:
Name oldpathname As newpathname

This assumes none of the New names will conflict with existing names.

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" & bk.Worksheets( _
1).Range("A1").Text & ".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

--
Regards,
Tom Ogilvy



"Filo" wrote in message
...
What is the best way to do the following:

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"

Note: I don't need to keep the original file name.

Please help!!! Thank you!






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Help, I have a deadline: Renaming Files in Folder

Tom,

When I tried the program, it didn't work. IT runs but it doesn't perform
anything. Could you check what may be wrong please?

Thank you.

Filo

"Tom Ogilvy" wrote:

I missed the first 5 characters. Here is a revision:

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

I assume the first 5 digits are the first 5 characters in the cell.

--
Regards,
Tom Ogilvy

"Tom Ogilvy" wrote in message
...
Use the Name command. Format:
Name oldpathname As newpathname

This assumes none of the New names will conflict with existing names.

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" & bk.Worksheets( _
1).Range("A1").Text & ".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

--
Regards,
Tom Ogilvy



"Filo" wrote in message
...
What is the best way to do the following:

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"

Note: I don't need to keep the original file name.

Please help!!! Thank you!






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 excel files in a folder Rhett C[_2_] Excel Discussion (Misc queries) 1 January 29th 10 05:23 AM
Pulling pdf files from general folder to specific folder [email protected] Excel Discussion (Misc queries) 2 September 8th 09 09:41 PM
Renaming Files Gordon[_2_] Excel Programming 1 August 1st 06 08:52 AM
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven Excel Discussion (Misc queries) 1 January 24th 06 03:28 PM
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven[_2_] Excel Programming 1 January 24th 06 04:23 AM


All times are GMT +1. The time now is 09:23 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"