PC Review


Reply
Thread Tools Rate Thread

ActiveWorksheet vs Worksheets.Item(1)

 
 
MP
Guest
Posts: n/a
 
      6th Dec 2007
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


 
Reply With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      6th Dec 2007
Hi Mark,

Typically just after loading a template the activesheet would be the first
sheet in the template, ie first tab, though not necessarily depending on how
the template was saved.

change

> 'Set oWs = oExcelApp.ActiveWorkSheet


to
Set oWs = oExcelApp.ActiveSheet

Note Activesheet could also be a Chart sheet

Regards,
Peter T




"MP" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
JE McGimpsey
Guest
Posts: n/a
 
      6th Dec 2007
"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 <(E-Mail Removed)>,
"MP" <(E-Mail Removed)> 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

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      6th Dec 2007
Seeing JE McGimpsey's response makes me realize I didn't fully digest your
question, in particular that you want to reference the first worksheet
irrespective as to which is the Activsheet. Your code was right first time,
stick with -

Set oWs = oWb.Worksheets.Item(1)
or
Set oWs = oWb.Worksheets(1)

and for future refernce keep in mind your 'ActiveWorkSheet' should have read
ActiveSheet

Regards,
Peter T


"Peter T" <peter_t@discussions> wrote in message
news:(E-Mail Removed)...
> Hi Mark,
>
> Typically just after loading a template the activesheet would be the first
> sheet in the template, ie first tab, though not necessarily depending on

how
> the template was saved.
>
> change
>
> > 'Set oWs = oExcelApp.ActiveWorkSheet

>
> to
> Set oWs = oExcelApp.ActiveSheet
>
> Note Activesheet could also be a Chart sheet
>
> Regards,
> Peter T
>
>
>
>
> "MP" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
MP
Guest
Posts: n/a
 
      6th Dec 2007
Thanks Peter and JE
Good points and infomation...

"Peter T" <peter_t@discussions> wrote in message
news:uY$(E-Mail Removed)...
> Seeing JE McGimpsey's response makes me realize I didn't fully digest your
> question, in particular that you want to reference the first worksheet
> irrespective as to which is the Activsheet. Your code was right first
> time,
> stick with -
>
> Set oWs = oWb.Worksheets.Item(1)


Guess I'll stick with this to avoid possibility someone saved template with
different sheet active...
Thanks for the pointer

> or
> Set oWs = oWb.Worksheets(1)


I always try to avoid default properties...lots of experts advise against
that on vb groups...in case of future revisions to object model I
suppose(however unlikely that might be)
:-)

>
> and for future refernce keep in mind your 'ActiveWorkSheet' should have
> read
> ActiveSheet


Yeah! Duh! :-)

>
> Regards,
> Peter T
>
>



 
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
ActiveWorksheet.Sections(1) does not work Jess Microsoft Excel Programming 1 2nd Oct 2009 04:58 PM
Can I save the activeworksheet only exploringmacro Microsoft Excel Programming 11 17th Jun 2009 07:02 AM
Keyboard Shortcut to toggle b/w Find and Replace and ActiveWorksheet? Brooks Microsoft Excel Discussion 4 1st Dec 2008 01:00 AM
Keyboard Shortcut to toggle b/w Find and Replace and ActiveWorksheet? Brooks Microsoft Excel Misc 0 25th Nov 2008 02:20 PM
Copy ActiveWorksheet Columns B,C,F to Another Worksheet in Workboo =?Utf-8?B?Sm9lIEsu?= Microsoft Excel Programming 1 5th Oct 2007 01:38 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:08 PM.