Making name of worksheet a function

  • Thread starter Thread starter Anna in Istanbul
  • Start date Start date
A

Anna in Istanbul

sheetHi everyone,

I was wondering if there is any way to have the name of a worksheet change
as a function of an entry in a specific cell. The sheets themselves are
created manually.

Specifically, I have a data entry sheet where the names (ie locations) of
some new branches are entered once. Then each location has a sheet, which now
carries the (fixed) branch code. But for more clarity, the customer would
prefer to see the branch names here without having to enter them in each
sheet name again.
 
This assumes you have a worksheet with a list of names in column A. If you
enter a name in column A, this macro will create a new worksheet with the
name just entered.

If an old name is re-entered, a warning is issued. Insert this event macro
in the worksheet code area of the worksheet with the list:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim a As Range, sh As Worksheet, v As String, s As Worksheet
Set t = Target
Set a = Range("A:A")
Set s = ActiveSheet
If Intersect(t, a) Is Nothing Then Exit Sub
v = t.Value
For Each sh In Worksheets
If v = sh.Name Then
MsgBox ("Worksheet " & v & " already exists")
Exit Sub
End If
Next
Worksheets.Add
ActiveSheet.Name = v
s.Activate
End Sub


Because it is worksheet code, it is very easy to install and automatic to use:

1. right-click the tab name near the bottom of the Excel window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you have any concerns, first try it on a trial worksheet.

If you save the workbook, the macro will be saved with it.


To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm
 
Back
Top