Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

Set rngA = Sheets(2).Range("A:A")

Does Sheets(2) mean the 2nd tab from the left?


In VBE, under Microsoft Excel Objects,
the 2nd tab appears as: Sheet2 (Sheet2)

What is the first instance of Sheet2 in the above ?

I gather that the 2nd instance -- within parens -- is the tabname, which can
be changed by user.

How could use be made of the 1st instance which apparently cannot be changed
and is more robust? An example line or 2 illustrating this usage would be
welcomed.

Thanks.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

The codename is that name to the left. The worksheet name (inside ()'s) is the
name you see on the worksheet tab.

The codename can be changed in code or via the properties window in the VBE.
But it is much more difficult for the average user to change that codename.

So when you have a worksheet named "Prices", but with code name of PricesWks,
you could use:

priceswks.range("a1").value = 1234.23

If you had done something like:

worksheets("Prices").range("a1").value = ...

And the user changes the name on the tab, then the code will break.

And yep. Sheets(2) is the second sheet counting from the left.

Max wrote:

Set rngA = Sheets(2).Range("A:A")

Does Sheets(2) mean the 2nd tab from the left?

In VBE, under Microsoft Excel Objects,
the 2nd tab appears as: Sheet2 (Sheet2)

What is the first instance of Sheet2 in the above ?

I gather that the 2nd instance -- within parens -- is the tabname, which can
be changed by user.

How could use be made of the 1st instance which apparently cannot be changed
and is more robust? An example line or 2 illustrating this usage would be
welcomed.

Thanks.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

The codename can be changed in code ...

An example or 2 of how the above could be done, Dave?

Thanks
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

ThisWorkbook.VBProject.VBComponents("Sheet1").Name = "NewCodeName"
where sheet1 is the code name

or
dim wks as worksheet
set wks = activesheet
ThisWorkbook.VBProject.VBComponents(wks.codename). Name = "NewCodeName"
'or
ThisWorkbook.VBProject.VBComponents(wks.CodeName) _
.Properties("_CodeName").Value = "NewCodeName2"


But you'll have to toggle a security setting
tools|macro|security|trusted publisher tab
check "trust access to Visual basic project"

(added in xl2002 IIRC--who knows where it's hiding in xl2007 <vbg.)


Max wrote:

The codename can be changed in code ...


An example or 2 of how the above could be done, Dave?

Thanks
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

Ps.

I saw a note from Chip Pearson about the first version.
Don't use it in xl97.
http://support.microsoft.com/kb/q172500/

Going through the .properties("_Codename") is ok, though.

Dave Peterson wrote:

ThisWorkbook.VBProject.VBComponents("Sheet1").Name = "NewCodeName"
where sheet1 is the code name

or
dim wks as worksheet
set wks = activesheet
ThisWorkbook.VBProject.VBComponents(wks.codename). Name = "NewCodeName"
'or
ThisWorkbook.VBProject.VBComponents(wks.CodeName) _
.Properties("_CodeName").Value = "NewCodeName2"

But you'll have to toggle a security setting
tools|macro|security|trusted publisher tab
check "trust access to Visual basic project"

(added in xl2002 IIRC--who knows where it's hiding in xl2007 <vbg.)

Max wrote:

The codename can be changed in code ...


An example or 2 of how the above could be done, Dave?

Thanks
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---


--

Dave Peterson


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

Thanks for all the clarifications, Dave.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

Some quick testing reveals that yes the sheets are ordered in their workbook
position. So when I used either this loop
For Each s In ActiveWorkbook.Sheets
or
For c = 1 To ActiveWorkbook.Sheets.Count
The sheets processed in the exact same order, from first sheet showing on
the left to the last one showing on the right in tab order.




"Max" wrote:

Set rngA = Sheets(2).Range("A:A")

Does Sheets(2) mean the 2nd tab from the left?


In VBE, under Microsoft Excel Objects,
the 2nd tab appears as: Sheet2 (Sheet2)

What is the first instance of Sheet2 in the above ?

I gather that the 2nd instance -- within parens -- is the tabname, which can
be changed by user.

How could use be made of the 1st instance which apparently cannot be changed
and is more robust? An example line or 2 illustrating this usage would be
welcomed.

Thanks.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

I skimped on that last reply.

To refer to a sheet directly, you would use:

Sheets("Sheetname")

and a range on that sheet would be for example

Sheets("Sheetname").Range("A1:B2")

You can/should of course precede Sheets with a workbook object. I would also
suggest using the Cells convention as my experience has been it is somewhat
more efficient and easier to program for processing once you get used to it.
The two lines below are equivalent:

Sheets("Sheetname").Range("A1")
Sheets("Sheetname").Cells(1, 1)

Also note: Sheets is a convention that refers to any sheet, either Worksheet
or a Chart sheet. Using Worksheets intead of Sheets (in For loops especially)
will refer to only the worksheet type of sheet.




"Max" wrote:

Set rngA = Sheets(2).Range("A:A")

Does Sheets(2) mean the 2nd tab from the left?


In VBE, under Microsoft Excel Objects,
the 2nd tab appears as: Sheet2 (Sheet2)

What is the first instance of Sheet2 in the above ?

I gather that the 2nd instance -- within parens -- is the tabname, which can
be changed by user.

How could use be made of the 1st instance which apparently cannot be changed
and is more robust? An example line or 2 illustrating this usage would be
welcomed.

Thanks.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---



  #9   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

Thanks for the views. Appreciated.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

Sheets is the collection object, of course.

Inside the parentheses:
(1), (2), etc. are the index numbers for the collection array which is
automatically assigned by the system in the order that they appear at the
bottom of your screen, no matter what names or numbers appear on the tabs.
You can verify this by opening the VB editor and you will see them in the
same order, unless someone has changed the code name. Even if someone
changes the code name, it will still sort in the sequence of the code name to
apply the index number.

("Sheet1"), ("Sheet2"), etc. the default name on the tab can be changed to
whatever you want but the index number remains the same.

Don't get into the code name, if you can help it.

"Max" wrote:

Set rngA = Sheets(2).Range("A:A")

Does Sheets(2) mean the 2nd tab from the left?


In VBE, under Microsoft Excel Objects,
the 2nd tab appears as: Sheet2 (Sheet2)

What is the first instance of Sheet2 in the above ?

I gather that the 2nd instance -- within parens -- is the tabname, which can
be changed by user.

How could use be made of the 1st instance which apparently cannot be changed
and is more robust? An example line or 2 illustrating this usage would be
welcomed.

Thanks.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---





  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

You may find that using the codename makes your procedure much more robust than
using the worksheet name. I don't think I'd warn people to stay away from using
it.



JLGWhiz wrote:
<<snipped

Don't get into the code name, if you can help it.

  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?


Once bitten, twice shy...
I had difficulties using codenames a few years back and since then never
use them. I prefer to set an object reference to a sheet and use that
through out the code.
(the above ought to help this discussion really get going, maybe it will work as
well as recommending Database functions instead of SumProduct formulas) <g
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)



"Dave Peterson"
wrote in message
You may find that using the codename makes your procedure much more robust than
using the worksheet name. I don't think I'd warn people to stay away from using
it.



JLGWhiz wrote:
<<snipped

Don't get into the code name, if you can help it.

  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

Do you protect the structure of every workbook that you develop so that users
can't rename the worksheets?

Or do you find the correct worksheet some other way -- in case that worksheet is
renamed?



Jim Cone wrote:


Once bitten, twice shy...
I had difficulties using codenames a few years back and since then never
use them. I prefer to set an object reference to a sheet and use that
through out the code.
(the above ought to help this discussion really get going, maybe it will work as
well as recommending Database functions instead of SumProduct formulas) <g
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)

"Dave Peterson"
wrote in message
You may find that using the codename makes your procedure much more robust than
using the worksheet name. I don't think I'd warn people to stay away from using
it.

JLGWhiz wrote:
<<snipped

Don't get into the code name, if you can help it.


--

Dave Peterson
  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?


There ain't no easy way. Am I the only one who has found the use of code
names unreliable? It would make life much easier to be able to use them.
Using object references, identifying sheets by a unique feature,
and using a VerifySheetExists function..."Can't find the Total Tally Sheet"
can all work.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)


"Dave Peterson"
wrote in message
Do you protect the structure of every workbook that you develop so that users
can't rename the worksheets?
Or do you find the correct worksheet some other way -- in case that worksheet is
renamed?



Jim Cone wrote:


Once bitten, twice shy...
I had difficulties using codenames a few years back and since then never
use them. I prefer to set an object reference to a sheet and use that
through out the code.
(the above ought to help this discussion really get going, maybe it will work as
well as recommending Database functions instead of SumProduct formulas) <g
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)




"Dave Peterson"
wrote in message
You may find that using the codename makes your procedure much more robust than
using the worksheet name. I don't think I'd warn people to stay away from using
it.

JLGWhiz wrote:
<<snipped

Don't get into the code name, if you can help it.

--
Dave Peterson
  #15   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

Using is one thing, changing them is another. There is no visibility if they
are changed, without going to the VB editor project window. My preference is
to stick with stuff that I can see, unless I want to purposely hide something.

"Dave Peterson" wrote:

You may find that using the codename makes your procedure much more robust than
using the worksheet name. I don't think I'd warn people to stay away from using
it.



JLGWhiz wrote:
<<snipped

Don't get into the code name, if you can help it.




  #16   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

I don't think I've ever seen a "normal" user change the codename of a sheet. So
in my situation, the only way a codename is changed is by the developer (usually
me). And that provides all the visibility I need.



JLGWhiz wrote:

Using is one thing, changing them is another. There is no visibility if they
are changed, without going to the VB editor project window. My preference is
to stick with stuff that I can see, unless I want to purposely hide something.

"Dave Peterson" wrote:

You may find that using the codename makes your procedure much more robust than
using the worksheet name. I don't think I'd warn people to stay away from using
it.



JLGWhiz wrote:
<<snipped

Don't get into the code name, if you can help it.



--

Dave Peterson
  #17   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 390
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

In , Dave Peterson
spake thusly:

I don't think I've ever seen a "normal" user change the codename
of a sheet. So in my situation, the only way a codename is
changed is by the developer (usually me). And that provides all
the visibility I need.


Hmm. I change the codenames fairly often. I'm somewhat of
a power user, but still "normal." My VBA skills are very
intermediate, but improving at pace. (Thanks, partly, to these
great groups.)

--
dman
  #18   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default Sheets(2) = 2nd tab? = Sheet2 (Sheet2)?

Thanks for the views. Appreciated.

I'm still tracking the flow-through discussions <g
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---


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
copy data from sheet2 to sheet1 when sheet2 has variable # of rows Anne Excel Discussion (Misc queries) 6 February 27th 09 09:48 PM
macro to print sheet2 without open sheet2 ramzi Excel Discussion (Misc queries) 1 January 28th 09 12:07 PM
how do copy "sheet1!A1+1 in sheet2 to sheet 3 and get "sheet2!A1+ Dany Excel Discussion (Misc queries) 5 April 16th 07 03:27 AM
Go to sheet2 hme Excel Programming 1 May 3rd 06 09:50 AM
first name and a second name - In Sheet2 I only need the first name tegger Excel Programming 2 September 26th 03 05:53 PM


All times are GMT +1. The time now is 05:19 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"