Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default Worksheet name


Excel Experts,

I have a workbook which has a sheet named "BskTrades". I want my code to
add a new sheet after BskTrades and name it "BskTotals".

My code adds a new sheets after BskTrades but fails on the second line which
I want to name the new sheet "BskTotals".

What do I have wrong?

Sub AddSheet()

Sheets.Add After:=Worksheets("BskTrades")

Worksheets(Worksheets("BskTrades").Count + 1).Name = "BskTotals"

End Sub

Thanks in advance,
Alan

--
achidsey
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Worksheet name

When you add a sheet by default it becomes the active sheet. YOu can just use
code like this...

Sheets.Add After:=Worksheets("BskTrades")
ActiveSheet.Name = "BskTotals"

You should however have some error handling on this code. If a sheet by that
name exists, then the code will crash when you try to rename.
--
HTH...

Jim Thomlinson


"achidsey" wrote:


Excel Experts,

I have a workbook which has a sheet named "BskTrades". I want my code to
add a new sheet after BskTrades and name it "BskTotals".

My code adds a new sheets after BskTrades but fails on the second line which
I want to name the new sheet "BskTotals".

What do I have wrong?

Sub AddSheet()

Sheets.Add After:=Worksheets("BskTrades")

Worksheets(Worksheets("BskTrades").Count + 1).Name = "BskTotals"

End Sub

Thanks in advance,
Alan

--
achidsey

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Worksheet name

not sure if this is the best way, but works for me

Sub AddSheet()

With Worksheets("BskTrades")
Sheets.Add After:=Worksheets("BskTrades")
End With
ActiveSheet.Name = "BskTotals"

End Sub


--


Gary


"achidsey" (notmorespam) wrote in message
...

Excel Experts,

I have a workbook which has a sheet named "BskTrades". I want my code to
add a new sheet after BskTrades and name it "BskTotals".

My code adds a new sheets after BskTrades but fails on the second line
which
I want to name the new sheet "BskTotals".

What do I have wrong?

Sub AddSheet()

Sheets.Add After:=Worksheets("BskTrades")

Worksheets(Worksheets("BskTrades").Count + 1).Name = "BskTotals"

End Sub

Thanks in advance,
Alan

--
achidsey



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Worksheet name

An individual worksheet (Worksheets("BskTrades")) doesn't have a .Count
property. If "BskTrades" is ALWAYS the last worksheet in the workbook,
you could use

Sheets.Add After:=Worksheets("BskTrades")
Worksheets(Worksheets.Count).Name = "BskTotals"

But you're better off using a object variable (either declared

Dim ws As Worksheet
Set ws = Worksheets.Add(After:=Worksheets("BskTrades"))
ws.Name = "BskTotal"

or temporary, using With...End With):

With Sheets.Add(After:=Worksheets("BskTrades"))
.Name = "BskTotal"
End With

or you could do it in one go:

Sheets.Add(After:=Worksheets("BskTrades")).Name = "BskTotal"

In article ,
"achidsey" (notmorespam) wrote:

Excel Experts,

I have a workbook which has a sheet named "BskTrades". I want my code to
add a new sheet after BskTrades and name it "BskTotals".

My code adds a new sheets after BskTrades but fails on the second line which
I want to name the new sheet "BskTotals".

What do I have wrong?

Sub AddSheet()

Sheets.Add After:=Worksheets("BskTrades")

Worksheets(Worksheets("BskTrades").Count + 1).Name = "BskTotals"

End Sub

Thanks in advance,
Alan

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default Worksheet name


Jim and Gary,

Thanks much for the assistance.

Alan

--
achidsey


"achidsey" wrote:


Excel Experts,

I have a workbook which has a sheet named "BskTrades". I want my code to
add a new sheet after BskTrades and name it "BskTotals".

My code adds a new sheets after BskTrades but fails on the second line which
I want to name the new sheet "BskTotals".

What do I have wrong?

Sub AddSheet()

Sheets.Add After:=Worksheets("BskTrades")

Worksheets(Worksheets("BskTrades").Count + 1).Name = "BskTotals"

End Sub

Thanks in advance,
Alan

--
achidsey

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
automatically appending newly added data on worksheet to a master list worksheet tabylee via OfficeKB.com Links and Linking in Excel 0 December 17th 09 04:24 PM
plot graph from multiple worksheet as embedded chart object on every worksheet [email protected] Excel Discussion (Misc queries) 2 August 24th 06 05:26 PM
Upload multiple text files into 1 excel worksheet + put the filename as the first column in the worksheet Aster Excel Worksheet Functions 3 March 12th 06 09:58 AM
copy range on every worksheet (diff names) to a master worksheet (to be created) Bernie[_2_] Excel Programming 2 September 22nd 04 03:30 PM
Attaching a JET database to an Excel Worksheet OR storing large binary data in a worksheet Ant Waters Excel Programming 1 September 3rd 03 11:34 AM


All times are GMT +1. The time now is 07:50 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"