Greetings experts,
I have created a VBA routine which will be embedded into a command button for a Userform, however, it is not very versatile.
Here is an example file: example.xlsm
The full code:
In the abstract below, I am trying to rename the "ESD Trf Qty" tabs I have coded it to be renamed like this: The part before “ Trf Qty”_Cell C28’s value. For example, if EVNL Trf Qty tab’s cell C28 value is A123 - LIFO then the tab should be renamed to “EVNL_A123 - LIFO”. However, it is not the most versatile, as I would need to add a similar paragraph of code for all tabs and there could be hundreds of tabs. I want the code to recognize all tabs which end with " Trf Qty" to be renamed like that.
Similarly, I also want to make this part more versatile. For the tabs which are named "By Ctrn-EIN" I rename it to “CellE25Value_CellC28Value”. If Cell E25 Value’s is Canada and Cell C28’s Value is B987 -123 then the tab should be renamed to “Canada_B987 - 123” I want the code to recognize all tabs which start with "By CTRN-" to be renamed like that.
Any help is much appreciated!
I have created a VBA routine which will be embedded into a command button for a Userform, however, it is not very versatile.
Here is an example file: example.xlsm
The full code:
Code:
Sub RenameWorkSheets()
Dim ws As Worksheet
'Rename Allocation
Set ws = getWorkSheet("Allocation")
If Not ws Is Nothing Then
renameWorkSheet ws, "Master_" & ws.Range("D28").Value
End If
'Rename ESD Trf Qty
Set ws = getWorkSheet("ESD Trf Qty")
If Not ws Is Nothing Then
renameWorkSheet ws, "ESD_" & ws.Range("C28").Value
End If
'Rename By Ctrn-EIN
Set ws = getWorkSheet("By Ctrn-EIN")
If Not ws Is Nothing Then
renameWorkSheet ws, ws.Range("E25").Value & ws.Range("C28").Value
End If
'Your other worksheets
End Sub
Function getWorkSheet(ByVal WorkSheetName As String) As Worksheet
On Error GoTo EH
Set getWorkSheet = Worksheets(WorkSheetName)
Exit Function
EH:
Set getWorkSheet = Nothing
End Function
Function renameWorkSheet(ByRef ws As Worksheet, ByVal NewName As String) As Boolean
On Error GoTo EH
If getWorkSheet(NewName) Is Nothing Then
ws.Name = NewName
renameWorkSheet = True
Else
'New Worksheet Name already exists
renameWorkSheet = False
End If
Exit Function
EH:
renameWorkSheet = False
End Function
In the abstract below, I am trying to rename the "ESD Trf Qty" tabs I have coded it to be renamed like this: The part before “ Trf Qty”_Cell C28’s value. For example, if EVNL Trf Qty tab’s cell C28 value is A123 - LIFO then the tab should be renamed to “EVNL_A123 - LIFO”. However, it is not the most versatile, as I would need to add a similar paragraph of code for all tabs and there could be hundreds of tabs. I want the code to recognize all tabs which end with " Trf Qty" to be renamed like that.
Code:
'Rename ESD Trf Qty
Set ws = getWorkSheet("ESD Trf Qty")
If Not ws Is Nothing Then
renameWorkSheet ws, "ESD_" & ws.Range("C28").Value
End If
Similarly, I also want to make this part more versatile. For the tabs which are named "By Ctrn-EIN" I rename it to “CellE25Value_CellC28Value”. If Cell E25 Value’s is Canada and Cell C28’s Value is B987 -123 then the tab should be renamed to “Canada_B987 - 123” I want the code to recognize all tabs which start with "By CTRN-" to be renamed like that.
Code:
'Rename By Ctrn-EIN
Set ws = getWorkSheet("By Ctrn-EIN")
If Not ws Is Nothing Then
renameWorkSheet ws, ws.Range("E25").Value & ws.Range("C28").Value
End If
Any help is much appreciated!