Excel Worksheets collection

M

Mike Labosh

Greetings, all:

I am in the midst of teaching some C# and I do not have Excel in front of
me, but a student posed a question about how to refer to a given worksheet
inside a workbook.

Can I say something like this?

exlApp.Workbooks(0).Worksheets(0)...

to refer to the first worksheet of the first workbook?

Can someone verify the syntax?

Are these collections0-based or 1-based?

--
Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com
"Escriba coda ergo sum." -- vbSensei
 
J

Jim Rech

Both collections are 1 based. The workbook indexing apparently depends on
the order opened so it would be pretty unreliable. You should use the
workbook's name Workbooks("abc.xls") or ActiveWorkbook.

I almost always use Worksheets("Sheetname"), if I know the name, in
preference to an index. Within a project though I'd first use the sheet's
VB Codename since it does not change with sheet order or name. That is not
easy to use outside of the project though.
 
Top