H
Harry Flashman
I would like a macro which allows me to rename existing worksheets
according to the values in a table (located on a table of contents
worksheet). The existing worksheet names might be contained (for
example) in A1:A10 and the new names in B1:B10.
I have some macro's already that perform similar tasks:
I have a macro which allows me to create worksheets based on a table
(for example cells A1:A10), which creates the worksheets and orders
the worksheets as per the table,
I also have a macro, which allows me to sort the worksheet according
to the table. This means I can change the order in the table (which
contains the worksheet names), and then sort the worksheets
accordingly.
Here are the macros: (the second macro is the one I think can be
altered to do what I want).
Sub NewWorkSheets()
' before this macro is run make sure the first worksheet is named
"Table of Contents"
' this macro will insert new worksheets in the correct order
On Error Resume Next
Dim Arr As Variant
Dim i As Long
Dim NewSheet As Worksheet
Arr = Selection.Value
For i = LBound(Arr) To UBound(Arr)
Set NewSheet = Sheets.Add
NewSheet.Name = Arr(i, 1)
Next i
Sheets("Table of Contents").Select
Sheets("Table of Contents").Move Before:=Sheets(1)
'Sort worksheets based on table on worksheet 1
On Error Resume Next
Dim SortOrder As Variant
Dim Ndx As Long
With Selection
For Ndx = .Cells.Count To 1 Step -1
Worksheets(.Cells(Ndx).Value).Move after:=Worksheets(1)
Next Ndx
End With
Sheets("Table of Contents").Select
End Sub
The second macro is actually contained in the first but I have this
version in case I just want to sort the worksheets and not create new
ones.
Sub SortWorksheets()
'Sort worksheets based on table on worksheet 1
On Error Resume Next
Dim SortOrder As Variant
Dim Ndx As Long
With Selection
For Ndx = .Cells.Count To 1 Step -1
Worksheets(.Cells(Ndx).Value).Move after:=Worksheets(1)
Next Ndx
End With
Sheets("Table of Contents").Select
End Sub
The above macro is clever enough to recognize the worksheet names from
the table, and then move the worksheet. I was hoping it might have the
potential (with some modification) to rename the worksheet based on
the table. For example the worksheet names might be contained in cells
A1:A10, and the new names in B1:B10.
Alas I know very little about VBA. If anyone could help me I would
appreciate it.
Harry
according to the values in a table (located on a table of contents
worksheet). The existing worksheet names might be contained (for
example) in A1:A10 and the new names in B1:B10.
I have some macro's already that perform similar tasks:
I have a macro which allows me to create worksheets based on a table
(for example cells A1:A10), which creates the worksheets and orders
the worksheets as per the table,
I also have a macro, which allows me to sort the worksheet according
to the table. This means I can change the order in the table (which
contains the worksheet names), and then sort the worksheets
accordingly.
Here are the macros: (the second macro is the one I think can be
altered to do what I want).
Sub NewWorkSheets()
' before this macro is run make sure the first worksheet is named
"Table of Contents"
' this macro will insert new worksheets in the correct order
On Error Resume Next
Dim Arr As Variant
Dim i As Long
Dim NewSheet As Worksheet
Arr = Selection.Value
For i = LBound(Arr) To UBound(Arr)
Set NewSheet = Sheets.Add
NewSheet.Name = Arr(i, 1)
Next i
Sheets("Table of Contents").Select
Sheets("Table of Contents").Move Before:=Sheets(1)
'Sort worksheets based on table on worksheet 1
On Error Resume Next
Dim SortOrder As Variant
Dim Ndx As Long
With Selection
For Ndx = .Cells.Count To 1 Step -1
Worksheets(.Cells(Ndx).Value).Move after:=Worksheets(1)
Next Ndx
End With
Sheets("Table of Contents").Select
End Sub
The second macro is actually contained in the first but I have this
version in case I just want to sort the worksheets and not create new
ones.
Sub SortWorksheets()
'Sort worksheets based on table on worksheet 1
On Error Resume Next
Dim SortOrder As Variant
Dim Ndx As Long
With Selection
For Ndx = .Cells.Count To 1 Step -1
Worksheets(.Cells(Ndx).Value).Move after:=Worksheets(1)
Next Ndx
End With
Sheets("Table of Contents").Select
End Sub
The above macro is clever enough to recognize the worksheet names from
the table, and then move the worksheet. I was hoping it might have the
potential (with some modification) to rename the worksheet based on
the table. For example the worksheet names might be contained in cells
A1:A10, and the new names in B1:B10.
Alas I know very little about VBA. If anyone could help me I would
appreciate it.
Harry