Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default Adding counter to new worksheet

I am adding a record number into column A of a data matrix, and the
number of rows in the matrix varies. I have the following code to do
this:

With Worksheets("WorksheetName")
For i = 2 To .Range("B65536").End(xlUp).Row
Cells(i, 1).Value = i - 1
Next i
End With

My problem is the the macro just added the worksheet where the data
exists, so I don't know it's name. Since this macro is iterative and
creates several such sheets the worksheet name keeps changing. How
do I make this macro work when the worksheet name is different every
time?

Thanks for the help.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,173
Default Adding counter to new worksheet

Somewhere in the code if you are adding sheets you will have something like

Worksheets.Add

All you need to do is declare a worksheet object at the start of the routine

Dim theWorksheetINeedToKnowTheNameOf as Worksheet
'I'm a little over the top with the variable name but...

the when you add a sheet use

Set theWorksheetINeedToKnowTheNameOf =Worksheets.Add

Now you don't need to now the name you can just use the variable name

theWorksheetINeedToKnowTheNameOf .Range("B65536).End.......etc

You could of course gat the name using

shtName=theWorksheetINeedToKnowTheNameOf .Name

Worksheets(shtName).Range......etc

--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
DTHIS
web:
www.nickhodge.co.uk
blog: www.nickhodge.co.uk/blog/

FREE UK OFFICE USER GROUP MEETING, MS READING, 27th APRIL 2007
www.officeusergroup.co.uk

wrote in message
oups.com...
I am adding a record number into column A of a data matrix, and the
number of rows in the matrix varies. I have the following code to do
this:

With Worksheets("WorksheetName")
For i = 2 To .Range("B65536").End(xlUp).Row
Cells(i, 1).Value = i - 1
Next i
End With

My problem is the the macro just added the worksheet where the data
exists, so I don't know it's name. Since this macro is iterative and
creates several such sheets the worksheet name keeps changing. How
do I make this macro work when the worksheet name is different every
time?

Thanks for the help.


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
Adding Counter to column for number of identical file numbers Sam Excel Discussion (Misc queries) 1 September 16th 08 06:48 PM
Adding a counter to ranges wmureports Excel Programming 2 June 29th 06 06:40 PM
Counter in a worksheet Jason B Excel Worksheet Functions 3 January 26th 06 07:00 PM
Adding a counter to a User Form in Excel Pam Excel Programming 2 August 17th 04 02:55 PM
Linked worksheet Query Counter Ed Lisay Excel Programming 1 September 14th 03 08:43 PM


All times are GMT +1. The time now is 11:01 AM.

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

About Us

"It's about Microsoft Excel"