header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

We have a new sponsor!  Introducting Pragma Systems.  See the home page for details.

Excel and selecting a worsheet in a workbook
Last Post 08 Feb 2010 06:04 PM by GWHowarth88. 3 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
dathrillUser is Offline
New Member
New Member
Posts:4
Avatar

--
08 Feb 2010 04:21 PM  

About to throw myself off the building on this one.

What I am tryong to do:

create a var for month, day and year                    (no problem and working)
open a excel workbook                                       (no problem and working)
once open
select a worksheet in the workbook                     (not working)

I've tried the following (and diferent variants)

 
$excelFilePath = "C:\myfile.xls"
$excel = New-Object -comobject Excel.Application
$excel.Visible = $TRUE
$workbook = $excel.Workbooks.Open($excelFilePath)
$worksheet = $workbook.worksheets.Item(1)

For some reason the $worksheet = $workbook.worksheets.Item(1) is not behaving like I thought it would. Item(1) doesnt do anything. For me it's always opening on the 6th worksheet no matter what.

if I change the Item(1) to (2) = nothing, stills open with the 6th worksheet active

I've tried other things like:

worksheets.select(1)
worksheets.item(1).select

plus many other weird things that I knew wouldnt even work.

can anyone point me in the right direction.

I just need to select a worksheet once my excel doc is open.



Phil

GWHowarth88User is Offline
Basic Member
Basic Member
Posts:348
Avatar

--
08 Feb 2010 05:18 PM  
You were almost there:

$excelFilePath = "C:\myfile.xls"
$excel = New-Object -comobject Excel.Application
$excel.Visible = $TRUE
$workbook = $excel.Workbooks.Open($excelFilePath)
$worksheet = $workbook.worksheets.Item(1).Activate()
dathrillUser is Offline
New Member
New Member
Posts:4
Avatar

--
08 Feb 2010 06:01 PM  

thank you so much

GWHowarth88User is Offline
Basic Member
Basic Member
Posts:348
Avatar

--
08 Feb 2010 06:04 PM  
Sorry, I actually messed that up. This one stores the worksheet you want correctly:

$excelFilePath = "C:\myfile.xls"
$excel = New-Object -comobject Excel.Application
$excel.Visible = $TRUE
$workbook = $excel.Workbooks.Open($excelFilePath)
$workbook.worksheets.Item(1).Activate()
$worksheet = $workbook.worksheets.Item(1)
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 footer
footer