Maggi: if you will be doing other stuff to each of the 40 workbooks, then adding the macro to each workbook is probably OK. I recommend that we expand this to automatically sequence through all workbooks, but let's get started with how to rename the sheets in a single workbook.. The macro has lots of comments, so you can follow what it is doing.
1. Make Sure You Can Run Macros
Excel has a security setting to ensure that malicious macros can not run without your knowledge. Some companies set up their computers with Macro security set to High. This is very safe and very conservative, but also means that you can not run macros casually. Open any workbook and navigate to Tools | Macros | Security (that’s the sequence for Excel2000). Check Medium. Once this is done, this security setting will be in force for all workbooks you open. You can reset the security setting at any time
2. Create the Macro
a. Copy the above code.
b. Open any workbook.
c. Press Alt + F11 to open the Visual Basic Editor (VBE).
d. In the left side window, hi-lite the target spreadsheet [it will likely be called VBAProject(name.xls) where name is the name of the spreadsheet]
e. Select an existing code module for the target worksheet; or from the f. Menu, choose Insert | Module.
f. Paste the code into the right-hand code window.
g. Close the VBE, save the file if desired.
h. See “Test The Code” below
Sub RenameSheets()
'
'****************************************************************************************
' Title RenameSheets
' Target Application: MS Excel
' Function; renames workbook sheets according to defined rules
' Limitations: presently limited to what is hardcoded in this proc, i.e,
' 3 original sheet names can re renamed. Total number of
' sheets does not matter.
'
' To expand or contract number of original/new sheet names:
' 1. reset dimension of OrigNames array and NewNames array
' 2. set value of Num to that same number
' 3. add or delete assignment statesments where specific names
' are assigned to OrigNames and NewNames
'
' Passed Values: NONE
' Public/Private Variables used: NONE
' VBA procedures called: NONE
' External Files Accessed: NONE
' Orig Date 21-Mar-2005
' Orig Author MWE
' HISTORY
'
'****************************************************************************************
'
'
Dim I As Integer, Num As Integer, Count As Integer
Dim OrigNames(3) As String, NewNames(3) As String
Dim WS As Worksheet
'
' define # of original sheet names and their corresponding new names
'
Num = 3
OrigNames(1) = "1000"
OrigNames(2) = "1100"
OrigNames(3) = "1200"
NewNames(1) = "Price"
NewNames(2) = "Product"
NewNames(3) = "Location"
'
' loop through all sheets;
' if sheet name is equal to one of the names in OrigNames array,
' rename that sheet to the corresponding new name and
' increment the counter by 1
'
Count = 0
For Each WS In ActiveWorkbook.Worksheets
For I = 1 To Num
If WS.Name = OrigNames(I) Then
Count = Count + 1
WS.Name = NewNames(I)
Exit For
End If
Next I
Next WS
'
' sheet examination is complete
' output message
'
MsgBox "Sheet examination and renaming is complete." + Chr(10) + _
"# of sheets examined = " + Str(ActiveWorkbook.Sheets.Count) + Chr(10) + _
"# sheets renamed = " + Str(Count), vbInformation
End Sub
3. Test The Code
Go to Tools | Macro | Macros and double-click on RenameSheets
I have attached a zipped workbook with 6 tabs; 3 of which are named with your original names and 3 of which have other names. The RenameSheets macro is installed as per the instructions above.
If each spreadsheet you receive is completely new and you will thus have to add the macro to each spreadsheet each time, that may be faster than your current approach, but it will eventually be a pain. If that is the case, we should create a Master spreadsheet that will sequence through the new spreadsheets and rename the tabs.. Not hard to do.
Bookmarks