Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11
Default Rename Multiple Excel Workbooks based on cell contents

I have the following code below that is designed to save the active worksheet
based on cell contents.

I have a file with hundreds of workbooks that need to be renamed. Is there
a way to add to this code to make it so that all files in a specific folder
get rename based on the cell contents?

Here is the code:

sub save_it()
dim fname
with activeworkbook
fname = €œSL-€œ.worksheets("sheet1").range("F4").value & _
.worksheets("sheet1").range("F3").value &
_.worksheets(€œSheet1€).range(€œF1€).value &_
".xls"

..saveas fname
end with
end sub

Thanks for the help.

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 79
Default Rename Multiple Excel Workbooks based on cell contents

If the oldname and new name is in only one file, you can try something like
this

Sub save_it()

Dim NewName As String
Dim OldName As String
Dim FilePath As String
Dim countRow As Integer
Dim LastRow As Integer
Dim objSheet As Worksheet
Dim objWb As Workbook

Set objSheet = ActiveWorkbook.Sheets(1)

FilePath = objSheet.Parent.Path
If Right(FilePath, 1) < "\" Then FilePath = FilePath & "\"

objSheet.Range("A" & objSheet.Rows.Count).Activate
Selection.End(xlUp).Activate
LastRow = ActiveCell.Row

For countRow = 2 To LastRow
OldName = objSheet.Range("A" & countRow).Value 'the old name is in
column A
NewName = objSheet.Range("B" & countRow).Value 'the new name is in
column B
Set objWb = Workbooks.Open(FilePath & OldName)

objWb.Activate
objWb.SaveAs FilePath & NewName
objWb.Close
Kill FilePath & OldName
Set objWb = Nothing
Next

End Sub

If the new name is in the workbook that you have to rename, you can change
the line:
NewName = objSheet.Range("B" & countRow).Value
to something like this:

Set objWb = Workbooks.Open(FilePath & OldName)
objWb.Activate
NewName = "SL -" & objWb.Worksheets("sheet1").Range("F4").Value & _
objWb.Worksheets("sheet1").Range("F3").Value & _
objWb.Worksheets("Sheet1").Range("F1").Value & _
".xls"

--

Rodrigo Ferreira


"Scott Campbell" escreveu na
mensagem ...
I have the following code below that is designed to save the active
worksheet
based on cell contents.

I have a file with hundreds of workbooks that need to be renamed. Is
there
a way to add to this code to make it so that all files in a specific
folder
get rename based on the cell contents?

Here is the code:

sub save_it()
dim fname
with activeworkbook
fname = "SL-".worksheets("sheet1").range("F4").value & _
.worksheets("sheet1").range("F3").value &
_.worksheets("Sheet1").range("F1").value &_
".xls"

.saveas fname
end with
end sub

Thanks for the help.



  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11
Default Rename Multiple Excel Workbooks based on cell contents

I probably didn't describe my task properly.

I have a folder that contains hundreds of excel spreadsheets (workbooks).
Each workbook has 1 tab (worksheet)-- Sheet1
Each workbook is named something like "SL-Report 1540435" where the numbers
appear completely random.
Each report is a monthly report for a specific product and a specific country.

I need my macro to rename each workbook based the contents of 3 cells F4
(Product), F3 Month and F1 Country so that it looks something like this
SL-ProductNameMonth200xCountryName

I am thinking that I need some kind of Loop function in VB.

"Rodrigo Ferreira" wrote:

If the oldname and new name is in only one file, you can try something like
this

Sub save_it()

Dim NewName As String
Dim OldName As String
Dim FilePath As String
Dim countRow As Integer
Dim LastRow As Integer
Dim objSheet As Worksheet
Dim objWb As Workbook

Set objSheet = ActiveWorkbook.Sheets(1)

FilePath = objSheet.Parent.Path
If Right(FilePath, 1) < "\" Then FilePath = FilePath & "\"

objSheet.Range("A" & objSheet.Rows.Count).Activate
Selection.End(xlUp).Activate
LastRow = ActiveCell.Row

For countRow = 2 To LastRow
OldName = objSheet.Range("A" & countRow).Value 'the old name is in
column A
NewName = objSheet.Range("B" & countRow).Value 'the new name is in
column B
Set objWb = Workbooks.Open(FilePath & OldName)

objWb.Activate
objWb.SaveAs FilePath & NewName
objWb.Close
Kill FilePath & OldName
Set objWb = Nothing
Next

End Sub

If the new name is in the workbook that you have to rename, you can change
the line:
NewName = objSheet.Range("B" & countRow).Value
to something like this:

Set objWb = Workbooks.Open(FilePath & OldName)
objWb.Activate
NewName = "SL -" & objWb.Worksheets("sheet1").Range("F4").Value & _
objWb.Worksheets("sheet1").Range("F3").Value & _
objWb.Worksheets("Sheet1").Range("F1").Value & _
".xls"

--

Rodrigo Ferreira


"Scott Campbell" escreveu na
mensagem ...
I have the following code below that is designed to save the active
worksheet
based on cell contents.

I have a file with hundreds of workbooks that need to be renamed. Is
there
a way to add to this code to make it so that all files in a specific
folder
get rename based on the cell contents?

Here is the code:

sub save_it()
dim fname
with activeworkbook
fname = "SL-".worksheets("sheet1").range("F4").value & _
.worksheets("sheet1").range("F3").value &
_.worksheets("Sheet1").range("F1").value &_
".xls"

.saveas fname
end with
end sub

Thanks for the help.




  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 79
Default Rename Multiple Excel Workbooks based on cell contents

Sub save_it()

Dim NewName As String
Dim OldName As String
Dim FilePath As String
Dim objWb As Workbook
Dim objFso As Object 'New FileSystemObject
Dim objFolder As Object 'Folder
Dim objFile As Object 'File
Set objFso = CreateObject("Scripting.FileSystemObject")

'FilePath = Folder that have your workbooks
FilePath = "C:\YourFolderThatContainsWorkbooks\"

Set objFolder = objFso.GetFolder(FilePath)

If Right(FilePath, 1) < "\" Then FilePath = FilePath & "\"


For Each objFile In objFolder.Files
OldName = objFile.Name
Set objWb = Workbooks.Open(FilePath & OldName)

objWb.Activate
NewName = "SL - " & objWb.Worksheets("sheet1").Range("F4").Value & _
objWb.Worksheets("sheet1").Range("F3").Value & _
objWb.Worksheets("Sheet1").Range("F1").Value & _
".xls"

objWb.SaveAs FilePath & NewName
objWb.Close
Kill FilePath & OldName
Set objWb = Nothing

Next

End Sub


--

Rodrigo Ferreira


"Scott Campbell" escreveu na
mensagem ...
I probably didn't describe my task properly.

I have a folder that contains hundreds of excel spreadsheets (workbooks).
Each workbook has 1 tab (worksheet)-- Sheet1
Each workbook is named something like "SL-Report 1540435" where the
numbers
appear completely random.
Each report is a monthly report for a specific product and a specific
country.

I need my macro to rename each workbook based the contents of 3 cells F4
(Product), F3 Month and F1 Country so that it looks something like this
SL-ProductNameMonth200xCountryName

I am thinking that I need some kind of Loop function in VB.

"Rodrigo Ferreira" wrote:

If the oldname and new name is in only one file, you can try something
like
this

Sub save_it()

Dim NewName As String
Dim OldName As String
Dim FilePath As String
Dim countRow As Integer
Dim LastRow As Integer
Dim objSheet As Worksheet
Dim objWb As Workbook

Set objSheet = ActiveWorkbook.Sheets(1)

FilePath = objSheet.Parent.Path
If Right(FilePath, 1) < "\" Then FilePath = FilePath & "\"

objSheet.Range("A" & objSheet.Rows.Count).Activate
Selection.End(xlUp).Activate
LastRow = ActiveCell.Row

For countRow = 2 To LastRow
OldName = objSheet.Range("A" & countRow).Value 'the old name is in
column A
NewName = objSheet.Range("B" & countRow).Value 'the new name is in
column B
Set objWb = Workbooks.Open(FilePath & OldName)

objWb.Activate
objWb.SaveAs FilePath & NewName
objWb.Close
Kill FilePath & OldName
Set objWb = Nothing
Next

End Sub

If the new name is in the workbook that you have to rename, you can
change
the line:
NewName = objSheet.Range("B" & countRow).Value
to something like this:

Set objWb = Workbooks.Open(FilePath & OldName)
objWb.Activate
NewName = "SL -" & objWb.Worksheets("sheet1").Range("F4").Value & _
objWb.Worksheets("sheet1").Range("F3").Value & _
objWb.Worksheets("Sheet1").Range("F1").Value & _
".xls"

--

Rodrigo Ferreira


"Scott Campbell" escreveu na
mensagem ...
I have the following code below that is designed to save the active
worksheet
based on cell contents.

I have a file with hundreds of workbooks that need to be renamed. Is
there
a way to add to this code to make it so that all files in a specific
folder
get rename based on the cell contents?

Here is the code:

sub save_it()
dim fname
with activeworkbook
fname = "SL-".worksheets("sheet1").range("F4").value & _
.worksheets("sheet1").range("F3").value &
_.worksheets("Sheet1").range("F1").value &_
".xls"

.saveas fname
end with
end sub

Thanks for the help.






  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11
Default Rename Multiple Excel Workbooks based on cell contents

Thank you very much Rodrigo

"Rodrigo Ferreira" wrote:

Sub save_it()

Dim NewName As String
Dim OldName As String
Dim FilePath As String
Dim objWb As Workbook
Dim objFso As Object 'New FileSystemObject
Dim objFolder As Object 'Folder
Dim objFile As Object 'File
Set objFso = CreateObject("Scripting.FileSystemObject")

'FilePath = Folder that have your workbooks
FilePath = "C:\YourFolderThatContainsWorkbooks\"

Set objFolder = objFso.GetFolder(FilePath)

If Right(FilePath, 1) < "\" Then FilePath = FilePath & "\"


For Each objFile In objFolder.Files
OldName = objFile.Name
Set objWb = Workbooks.Open(FilePath & OldName)

objWb.Activate
NewName = "SL - " & objWb.Worksheets("sheet1").Range("F4").Value & _
objWb.Worksheets("sheet1").Range("F3").Value & _
objWb.Worksheets("Sheet1").Range("F1").Value & _
".xls"

objWb.SaveAs FilePath & NewName
objWb.Close
Kill FilePath & OldName
Set objWb = Nothing

Next

End Sub


--

Rodrigo Ferreira


"Scott Campbell" escreveu na
mensagem ...
I probably didn't describe my task properly.

I have a folder that contains hundreds of excel spreadsheets (workbooks).
Each workbook has 1 tab (worksheet)-- Sheet1
Each workbook is named something like "SL-Report 1540435" where the
numbers
appear completely random.
Each report is a monthly report for a specific product and a specific
country.

I need my macro to rename each workbook based the contents of 3 cells F4
(Product), F3 Month and F1 Country so that it looks something like this
SL-ProductNameMonth200xCountryName

I am thinking that I need some kind of Loop function in VB.

"Rodrigo Ferreira" wrote:

If the oldname and new name is in only one file, you can try something
like
this

Sub save_it()

Dim NewName As String
Dim OldName As String
Dim FilePath As String
Dim countRow As Integer
Dim LastRow As Integer
Dim objSheet As Worksheet
Dim objWb As Workbook

Set objSheet = ActiveWorkbook.Sheets(1)

FilePath = objSheet.Parent.Path
If Right(FilePath, 1) < "\" Then FilePath = FilePath & "\"

objSheet.Range("A" & objSheet.Rows.Count).Activate
Selection.End(xlUp).Activate
LastRow = ActiveCell.Row

For countRow = 2 To LastRow
OldName = objSheet.Range("A" & countRow).Value 'the old name is in
column A
NewName = objSheet.Range("B" & countRow).Value 'the new name is in
column B
Set objWb = Workbooks.Open(FilePath & OldName)

objWb.Activate
objWb.SaveAs FilePath & NewName
objWb.Close
Kill FilePath & OldName
Set objWb = Nothing
Next

End Sub

If the new name is in the workbook that you have to rename, you can
change
the line:
NewName = objSheet.Range("B" & countRow).Value
to something like this:

Set objWb = Workbooks.Open(FilePath & OldName)
objWb.Activate
NewName = "SL -" & objWb.Worksheets("sheet1").Range("F4").Value & _
objWb.Worksheets("sheet1").Range("F3").Value & _
objWb.Worksheets("Sheet1").Range("F1").Value & _
".xls"

--

Rodrigo Ferreira


"Scott Campbell" escreveu na
mensagem ...
I have the following code below that is designed to save the active
worksheet
based on cell contents.

I have a file with hundreds of workbooks that need to be renamed. Is
there
a way to add to this code to make it so that all files in a specific
folder
get rename based on the cell contents?

Here is the code:

sub save_it()
dim fname
with activeworkbook
fname = "SL-".worksheets("sheet1").range("F4").value & _
.worksheets("sheet1").range("F3").value &
_.worksheets("Sheet1").range("F1").value &_
".xls"

.saveas fname
end with
end sub

Thanks for the help.







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
*How can you rename a tab based on a cell value Bob Phillips Excel Discussion (Misc queries) 18 January 2nd 08 08:31 PM
new worksheet and rename from cell contents macro Henry Excel Worksheet Functions 1 September 12th 06 10:35 AM
Combine contents of multiple workbooks into one worksheet EMG03 Excel Worksheet Functions 1 October 25th 05 12:15 AM
Formula to return cell contents based on multiple conditions Bill Excel Worksheet Functions 3 January 19th 05 09:59 AM
How do I automatically rename a sheet with the contents of a cell. michaelspearin Excel Discussion (Misc queries) 3 December 3rd 04 09:27 PM


All times are GMT +1. The time now is 02:04 AM.

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"