add 'name worksheet after cell' macro when creating sheet

zeegerd

New Member
Joined
Mar 21, 2012
Messages
5
Hi all,

In Excel (2003) I have this situation: one Summary worksheet, then a range of sheets containing project information.

From the Summary, with a button a macro can be launched to add a new project sheet. This works perfectly. But I would like to name the new sheet after a cell (say C3) on the new sheet dynamically, i.e. if C3 changes, the sheet name should change immediately, preferably. The initial value (when creating the sheet) may simply be 'Project name here'.

I found a macro that can do this (triggered by a change in that cell), but then this macro should be added automatically to that sheet when creating it, right? I didn't succeed in doing so. That one goes like this:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$3" Then
On Error Resume Next
Me.Name = Target
End If
End Sub

Any idea? Thanks for your time!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
as an option
add this code to yr Summary Sheet change event and play with it!
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim isect As Range
    Dim n As Integer
    Dim SheetExists As Boolean
    If Target.Cells.Count > 1 Then Exit Sub
    On Error GoTo ResetApplication
    Application.EnableEvents = False
    Set isect = Intersect(Target, Range("A:A"))
    If Not isect Is Nothing And Target <> "" Then
        For n = 1 To Sheets.Count
            If Sheets(n).Name = Target Then
                SheetExists = True
                Exit For
            End If
        Next
        If Not SheetExists Then
            ThisWorkbook.Sheets.Add after:=Worksheets(Worksheets.Count)
            ActiveSheet.Name = Target
            Me.Activate
            Target.Select
            ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", _
                                       SubAddress:="'" & Target & "'!A1", _
                                       TextToDisplay:=Target.Value
                                     
                    
        End If
    End If
ResetApplication:
    Err.Clear
    On Error GoTo 0
    Application.EnableEvents = True
    Set isect = Nothing
End Sub
 
Upvote 0
Instead of adding a sheet, copy the one that contains the macro you posted.

Hello Andrew,

Thanks, I guess I've tried that but without success. What happens is: the last (filled in) project sheet is copied, a new sheet is created, the last sheet is pasted and the values are emptied.

That way however, the macro for doing the job (if present in the last sheet) is not copied along onto the new project sheet.

Gr, Zeeger.
 
Upvote 0
as an option
add this code to yr Summary Sheet change event and play with it!

Hello Tim,

Thanks, that one is interesting but it works a bit differently.
One button/macro refreshes the Summary; another creates a new project sheet.

The other way around is also a reasonable solution, i.e. the sheet name is filled in and the cell shows that (project) name:
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,256)

But doing a refresh (e.g. F9) yields all project sheets to show the name of the selected project sheet...

I found a different solution, using VB creating a custom function which does the job:
http://www.ozgrid.com/VBA/return-sheet-name.htm

Thanks anyway.
 
Upvote 0
If you copy a worksheet its code is also copied.

Hi Andrew,

Well, how do you copy a sheet? Perhaps in a different way. In my case (in a macro of course): ctrl+A for the source sheet, ctrl+c, add new sheet, paste on new sheet, empty the necessary cells that will be filled in for the new project. That way, the code in the source sheet is not copied into the new sheet. That should be done automatically, either by 'simply' copying (which you seem to say) or describing in the macro how the sheet-specific macro should be added.

Btw I read that creating 'code with code' (i.e. automatically) may be a tricky thing: http://www.cpearson.com/excel/vbe.aspx

I'm a relative newbie in this field so if this sounds dumb somehow, you know who said it. ;)
 
Upvote 0
when you copy all the cells, rather than "add" a new sheet, why not plunk the values into a copy of the "template" sheet that contains (in the sheet's module) code wanted?
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top