Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

rename sheetname based on value of field in other sheet

Posted on 2013-12-26
2
Medium Priority
?
321 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:Michael Fowler
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 81

Accepted Solution

by:
byundt earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

916 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