View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
JE McGimpsey JE McGimpsey is offline
external usenet poster
 
Posts: 4,624
Default ActiveWorksheet vs Worksheets.Item(1)

"Proper" doesn't have much meaning - there are usually multiple ways to
accomplish a goal in XL/VBA. Your first method obviously works fine,
though you can also use the shortcut

Set oWs = oWb.Worksheets(1)

(it's compiled the same way, IIRC).

Since creating a workbook makes it the active workbook, you could also do

Set oWs = oExcelApp.ActiveSheet

as long as the template was saved with the first worksheet active
(otherwise the ActiveSheet will be whatever sheet was active when the
template was saved).

My preference is the former, but both are "proper".

In article ,
"MP" wrote:

Hi
Trying to learn automating excel via vba

Dim oWb as Workbook
Set oWb = oExcelApp.WorkBooks.Add (TEMPLATE_NAME)
'that works

Dim oWs as WorkSheet
Set oWs = oWb.Worksheets.Item(1)
'That works

so the above line is ok but I thought maybe this would be a "better" way
I assumed a new workbook would begin with the first sheet activated???
'Set oWs = oExcelApp.ActiveWorkSheet

'but it throws error (Object doesn't support this property or method)

What is the proper way to get the first sheet in a new workbook?
Thanks
Mark