Solved

rename sheetname based on value of field in other sheet

Posted on 2013-12-26
2
314 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 81

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…
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.

803 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