Help changing the sheet name

P

pete212

Hello everyone,

When you create a new sheet in Excell 2007, it calls it Sheet1 an
obviously this increments for each new sheet, the next one being Sheet2
Sheet3 etc.

My question is that is there anyway to change the default name it call
new sheets? The reason is, I have a Macro that creates a sheet, and the
renames it. The problem is, I can only run this Macro once.

Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "Pete"

If I run this Macro again, then the new sheet is called "Sheet3" and no
"Sheet2", so my Macro throws an error.

If I could change it so that every new sheet was called, for exampl
"new", then my Macro could always reference "new" and rename it t
"pete", no matter how many times its run.

Now your probably going to ask why I would want to run this more tha
once, as I cannot have two sheets called "Pete" anyway. Well, the Macr
firstly deletes the sheet called Pete, before doing a bunch of othe
stuff and lastly creating the sheet again. So there never is a situatio
where there would be more than one sheet called "Pete".

Is anyone able to help with this? An alternative solution would b
rather than delete the "Pete" sheet it just clears all the contents hel
in it - but the problem is if I do this, the sheet becomes empty a
expected but at the point where data is pasted in, the Macro throws
run time error and crashes my entire work book. I think this is a bug i
Excel itself as the macro runs fine if the sheet is brand new.

Any help is greatly appreciated.

Many thanks
 
C

Claus Busch

Hi Pete,

Am Mon, 14 May 2012 13:53:15 +0000 schrieb pete212:
Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "Pete"

the new sheet is the active sheet. Try:
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Pete"

Regards
Claus Busch
 
C

Claus Busch

Hi Pete,

Am Mon, 14 May 2012 13:53:15 +0000 schrieb pete212:
Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "Pete"

only one sheet can be named "Pete". Before you add a new sheet which
should be named "Pete", you have to rename the last sheet (Pete) with
another name:
Sheets(Sheets.Count).Name = "Sheet" & Sheets.Count
Sheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Pete"


Regards
Claus Busch
 
P

pete212

Brilliant Claus!

Thanks again for your help with these two issues! You've been a grea
help!

:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top