header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Excel and selecting a worsheet in a workbook
Last Post 08 Feb 2010 10:04 AM by George Howarth. 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 08:21 AM

    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

    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    08 Feb 2010 09:18 AM
    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 10:01 AM

    thank you so much

    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    08 Feb 2010 10:04 AM
    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.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer