Solved

rename sheetname based on value of field in other sheet

Posted on 2013-12-26
2
312 Views
Last Modified: 2013-12-27
Folks

How based on an input value from field b1 in sheet 1 can a macro change the name of a excel sheet  i.e. rename to "chosen continent"

if
sheet 1 b1  = europe
 
rename sheet 2 to "chosen continent"
rename sheet 3 to "americas"
rename sheet 4 to "asia"

if
sheet 1 b1  = americas
rename sheet 2 to "europe"
rename sheet 3 to "chosen continent"
rename sheet 4 to "asia"

if
sheet 1 b1  = asia
 
rename sheet 2 to "europe"
rename sheet 3 to "americas"
rename sheet 3 to "chosen continent"
0
Comment
Question by:rutgermons
2 Comments
 
LVL 23

Expert Comment

by:Michael74
ID: 39740977
Here is the VBA that follows your pseudo code.

Sub test()
   RenameSheet (Range("B1").value)
End Sub


Sub RenameSheet(value As String)
   If (value = "europe") Then
      Sheets(2).Name = "chosen continent"
      Sheets(3).Name = "americas"
      Sheets(4).Name = "asia"
   ElseIf (value = "americas") Then
      Sheets(2).Name = "europe"
      Sheets(3).Name = "chosen continent"
      Sheets(4).Name = "asia"
   ElseIf (value = "asia") Then
      Sheets(2).Name = "europe"
      Sheets(3).Name = "americas"
      Sheets(4).Name = "chosen continent"
   End If
End Sub

Open in new window

0
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
ID: 39741088
If you want the renaming to occur automatically, then consider a Worksheet_Change sub in the code pane for Sheet1.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B1")) Is Nothing Then
    On Error Resume Next
    Worksheets("chosen continent").Name = Array(, , "europe", "americas", "asia")(Worksheets("chosen continent").Index)
    Worksheets(Range("B1").Value).Name = "chosen continent"
    On Error GoTo 0
End If
End Sub

Open in new window

If instead, you want a simple macro for renaming (and don't need all the error handling provisions), then consider this two liner:
Sub SheetRenamer()
Worksheets("chosen continent").Name = Array(, , "europe", "americas", "asia")(Worksheets("chosen continent").Index)
Worksheets(Worksheets("Sheet1").Range("B1").Value).Name = "chosen continent"
End Sub

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now