pradeep.thakur
New Member
- Joined
- Jun 18, 2012
- Messages
- 8
Hi,
Im trying to create a macro to rename the sheets in a list of workbooks, if the names differ from a source list of sheet names. Here is the code
Whats it needs to do is,
Here is table of corresponding sheets
Green cells imply that the sheet is required
Red implies sheet is not required
Check verifies total with another source and returns difference
Please advise.
Thanks
Im trying to create a macro to rename the sheets in a list of workbooks, if the names differ from a source list of sheet names. Here is the code
Code:
Sub RenameSheets()
Dim w, x, y, Z As Integer
Dim tabs, lasttab, extractedtabs, str, stra, lastrow, lastbook As Range
Set tabs = Workbooks("Control").Sheets("Control").Range("Q7:Q31") ' List of desired tabs
Set lastbook = Workbooks("Control").Sheets("Control").Range("F7:F80") ' List of files to be modified
Set lastrow = lastbook.find("")
Set lasttab = tabs.find("")
w = lasttab.Row - 1
y = lastrow.Row - 1
For Z = 7 To y
Set extractedtabs = Workbooks("Control").Sheets("Macros").Range(Cells(Z, "H"), Cells(Z, "AF")) ' Problem setting this up as H7:AF7 (row increments for every file); refers to existing sheets in every file (pulled by another macro)
Application.DisplayAlerts = False
Workbooks.Open Filename:=Workbooks("Control").Sheets("Control").Cells(Z, "I").Text ' Path of file to be modified
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
Set str = tabs.find(sh.Name)
If str Is Nothing Then
For x = 7 To w
Set stra = extractedtabs.find(Cells(x, "Q"))
If stra Is Nothing Then
sh.Name = Workbooks("Control").Sheets("Control").Cells(x, "Q").Text
Exit For
Else
End If
Next
Else
End If
Next
Application.DisplayAlerts = True
ActiveWorkbook.Save
ActiveWindow.Close
Next
End Sub
Whats it needs to do is,
- Compare the sheet names with a standard list (a table of corresponding sheets for corresponding files).
- Then, if the active sheet name is not present in the desired list, rename it as per the list.
- Order is not important as another macro will have deleted sheets from the workbook (with exceptions which will not need to be renamed as they are already appropriately named) and then added new blank sheets based on specific totals for each file.
Here is table of corresponding sheets

Green cells imply that the sheet is required
Red implies sheet is not required
Check verifies total with another source and returns difference
Please advise.
Thanks