PC Review


Reply
Thread Tools Rate Thread

create named worksheet based on existing numeration

 
 
SteveDB1
Guest
Posts: n/a
 
      25th Feb 2008
Greetings all.
Hope your respective weekends were enjoyable.
I have recorded, and modified a macro that creates a worksheet within an
existing workbook. For now, I've just set the name of the worksheet to be "A."
I would like to now have this new worksheet named within the existing
framework of the workbook.
I.e., for this type of worksheet, we typically name them by a ordinal
number-- 1 through N.
In one workbook we may only be up to 5, and another could easily be upwards
of 70, or more.
Presently, the code for my worksheet name is:

Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = "A"

What would I use to make the name of the worksheet the next number inline
for the respective worksheet?
I appreciate your responses.
Best.
 
Reply With Quote
 
 
 
 
SteveDB1
Guest
Posts: n/a
 
      25th Feb 2008
Sometrhing I forgot to add.
I've tried

Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = sheets.count

And

Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = sheets.count+1

Only to realize that this also counts the hidden worksheets.
I've like to have it not count the hidden worksheets.

Again, thank you.

 
Reply With Quote
 
paul.robinson@it-tallaght.ie
Guest
Posts: n/a
 
      25th Feb 2008
Hi
If the first worksheet is "A1", the second "A2" and so on then use

Sheets(Sheets.Count).Name = "A"&Sheets.Count

If there are other worksheets in the workbook without this naming
structure then you will have to keep track of the last sheet number
and any deletion of numbered sheets. Could be a bit tricky in that
case, so get back if that is what you need.

regards
Paul

On Feb 25, 3:29*pm, SteveDB1 <Steve...@discussions.microsoft.com>
wrote:
> Greetings all.
> Hope your respective weekends were enjoyable.
> I have recorded, and modified a macro that creates a worksheet within an
> existing workbook. For now, I've just set the name of the worksheet to be "A."
> I would like to now have this new worksheet named within the existing
> framework of the workbook.
> I.e., for this type of worksheet, we typically name them by a ordinal
> number-- 1 through N.
> In one workbook we may only be up to 5, and another could easily be upwards
> of 70, or more.
> Presently, the code for my worksheet name is:
>
> Sheets.Add After:=Sheets(Sheets.Count)
> * Sheets(Sheets.Count).Name = "A"
>
> What would I use to make the name of the worksheet the next number inline
> for the respective worksheet?
> I appreciate your responses.
> Best.


 
Reply With Quote
 
michael.beckinsale
Guest
Posts: n/a
 
      25th Feb 2008
Hi SteveDB1,

There are a number of topics on this website if you do a search for
"count specific sheets"

However if ALL your sheets are numbered 1,2 ,3 4, etc then your next
sheet will be Sheet.Count + 1 if you do the count before creating the
new sheet, or simply Sheet.Count if after.

If you have other types of sheet names you might want to set up a
'counter & loop thru all the sheets testing to see if the name =
integer (you will have to convert the name to an integer as sheet
names are strings)

Hope this helps. Repost if you get suck

Regards

Michael.

 
Reply With Quote
 
Tim Williams
Guest
Posts: n/a
 
      26th Feb 2008
How about working backwards, looking for the last sheet, and then adding the
new one after that ?

Tim

'****************************************
Sub Tester()
Dim s As Worksheet, i As Integer
For i = 1 To 10
Set s = ActiveWorkbook.Sheets.Add
NameAndPos s
Next i
End Sub

Sub NameAndPos(sht As Worksheet)

Dim i As Integer, i2 As Integer
i2 = 1
For i = 100 To 1 Step -1
If SheetExists(sht.Parent, CStr(i)) Then
i2 = i + 1
Exit For
End If
Next i

With sht
.Name = CStr(i2)
If i2 <> 1 Then .Move After:=sht.Parent.Sheets(CStr(i))
End With

End Sub

Function SheetExists(wb As Excel.Workbook, s As String)
Dim o As Worksheet
On Error Resume Next
Set o = wb.Sheets(s)
On Error GoTo 0
SheetExists = Not o Is Nothing
End Function
'************************************

"michael.beckinsale" <(E-Mail Removed)> wrote in message
news:f4c37771-0554-42ae-b4a0-(E-Mail Removed)...
> Hi SteveDB1,
>
> There are a number of topics on this website if you do a search for
> "count specific sheets"
>
> However if ALL your sheets are numbered 1,2 ,3 4, etc then your next
> sheet will be Sheet.Count + 1 if you do the count before creating the
> new sheet, or simply Sheet.Count if after.
>
> If you have other types of sheet names you might want to set up a
> 'counter & loop thru all the sheets testing to see if the name =
> integer (you will have to convert the name to an integer as sheet
> names are strings)
>
> Hope this helps. Repost if you get suck
>
> Regards
>
> Michael.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter worksheet based on named range on alternate sheet fishy Microsoft Excel Programming 3 19th Feb 2010 09:11 PM
relative Named Ranges based on worksheet =?Utf-8?B?RnIuIFJvYmVydA==?= Microsoft Excel Worksheet Functions 5 2nd Jun 2009 08:27 PM
How to update a worksheet based on a defined named range using VB. Rambo Microsoft Excel Programming 0 23rd Mar 2008 07:32 AM
Create named ranges in Column on worksheet eholz1 Microsoft Excel Programming 0 24th Jan 2008 01:41 AM
Create Worksheet From Values in Existing Cells Using Existing Worksheet as Template. Ardy Microsoft Excel Programming 18 29th Nov 2006 03:23 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:05 AM.