Solved

rename sheetname based on value of field in other sheet

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

895 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

19 Experts available now in Live!

Get 1:1 Help Now