Windows Vista Forums

How to determin a worksheet exist in Excel?
  1. #1


    Michael Gao Guest

    How to determin a worksheet exist in Excel?

    Hi,
    I wrote a PS script like this:

    $a = New-Object -comobject Excel.Application
    $a.visible = $true
    $b = $a.Workbooks.Open("D:\out\M2.xlsx")
    $i = 1
    do {
    $c = $b.worksheets.Item($i)
    $c.name = $c.cells.item(2,2).value2
    $i++
    }while ($i -le $b.worksheets.count)

    Some value may be the same, so I got error:
    Exception setting "Name": "Cannot rename a sheet to the same name as another
    sheet, a referenced object library or a workbook referenced by Visual
    Basic."
    At line 7, position 4
    $c.name = $c.cells.item(2,2).value2



    Does anybody met the same problem?

    Thanks!

    MG


      My System SpecsSystem Spec

  2. #2


    Shay Levi Guest

    Re: How to determin a worksheet exist in Excel?


    Try:

    $xl = new-object -com excel.application
    $workbook = $xl.workbooks.open("D:\out\M2.xlsx")

    # filter workSheets by name
    $sheet = $workbook.worksheets | where {$_.name -eq "sheetName"}

    if($sheet) { "workSheet exists" }



    ---
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > Hi,
    > I wrote a PS script like this:
    > $a = New-Object -comobject Excel.Application
    > $a.visible = $true
    > $b = $a.Workbooks.Open("D:\out\M2.xlsx")
    > $i = 1
    > do {
    > $c = $b.worksheets.Item($i)
    > $c.name = $c.cells.item(2,2).value2
    > $i++
    > }while ($i -le $b.worksheets.count)
    > Some value may be the same, so I got error:
    > Exception setting "Name": "Cannot rename a sheet to the same name as
    > another
    > sheet, a referenced object library or a workbook referenced by Visual
    > Basic."
    > At line 7, position 4
    > $c.name = $c.cells.item(2,2).value2
    > Does anybody met the same problem?
    >
    > Thanks!
    >
    > MG
    >


      My System SpecsSystem Spec

  3. #3


    MG Guest

    Re: How to determin a worksheet exist in Excel?

    Sorry for my question is not quite clearly.
    I have an Excel file that contain hundreds of worksheets. I need change each
    worksheet name to their cells value (2,2). But some worksheets have same
    value in cell(2,2), forexample, both of sheet1,4,5,62 have same value "name"
    in cell(2,2). So only sheet1 change name to "name", other worksheets will
    get error and keep their own name.
    My question is, can I change these worksheets name to "name1"..."name34"
    base on my script? Or how can I determin whether a worksheets named as
    current cell(2,2) already exist before I change it?

    Thanks.
    MG

    "Shay Levi" <no@xxxxxx> wrote in message
    news:95d8089330ab78ca9b6ccefa9d40@xxxxxx

    >
    > Try:
    >
    > $xl = new-object -com excel.application
    > $workbook = $xl.workbooks.open("D:\out\M2.xlsx")
    >
    > # filter workSheets by name
    > $sheet = $workbook.worksheets | where {$_.name -eq "sheetName"}
    >
    > if($sheet) { "workSheet exists" }
    >
    >
    >
    > ---
    > Shay Levi
    > $cript Fanatic
    > http://scriptolog.blogspot.com
    >

    >> Hi,
    >> I wrote a PS script like this:
    >> $a = New-Object -comobject Excel.Application
    >> $a.visible = $true
    >> $b = $a.Workbooks.Open("D:\out\M2.xlsx")
    >> $i = 1
    >> do {
    >> $c = $b.worksheets.Item($i)
    >> $c.name = $c.cells.item(2,2).value2
    >> $i++
    >> }while ($i -le $b.worksheets.count)
    >> Some value may be the same, so I got error:
    >> Exception setting "Name": "Cannot rename a sheet to the same name as
    >> another
    >> sheet, a referenced object library or a workbook referenced by Visual
    >> Basic."
    >> At line 7, position 4
    >> $c.name = $c.cells.item(2,2).value2
    >> Does anybody met the same problem?
    >>
    >> Thanks!
    >>
    >> MG
    >>
    >
    >

      My System SpecsSystem Spec

  4. #4


    Marco Shaw [MVP] Guest

    Re: How to determin a worksheet exist in Excel?

    MG wrote:

    > Sorry for my question is not quite clearly.
    > I have an Excel file that contain hundreds of worksheets. I need change
    > each worksheet name to their cells value (2,2). But some worksheets have
    > same value in cell(2,2), forexample, both of sheet1,4,5,62 have same
    > value "name" in cell(2,2). So only sheet1 change name to "name", other
    > worksheets will get error and keep their own name.
    > My question is, can I change these worksheets name to "name1"..."name34"
    > base on my script? Or how can I determin whether a worksheets named as
    > current cell(2,2) already exist before I change it?
    It's doable, but it is asking for too many brain cycles for a weekend... ;-)

    Check back Monday or so, unless someone else beats me to a solution.

    Marco

    --
    Microsoft MVP - Windows PowerShell
    http://www.microsoft.com/mvp

    PowerGadgets MVP
    http://www.powergadgets.com/mvp

    Blog:
    http://marcoshaw.blogspot.com

      My System SpecsSystem Spec

  5. #5


    Shay Levi Guest

    Re: How to determin a worksheet exist in Excel?

    Hi MG,

    Try this:


    $xl = new-object -com excel.application
    #$xl.visible=$true
    $workbooks = $xl.workbooks.open("D:\out\M2.xlsx")
    $sheets = $workbooks.worksheets

    foreach($s in $sheets){
    $name = $s.range("B2").value()
    $counter=$null
    while($sheets | where {$_.name -eq "$name$counter"} ){ $counter++ }
    $s.name = "$name$counter"
    $counter=$null
    }

    $workbook.save()
    $xl.quit()



    ---
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    M> Sorry for my question is not quite clearly.
    M> I have an Excel file that contain hundreds of worksheets. I need
    M> change each
    M> worksheet name to their cells value (2,2). But some worksheets have
    M> same
    M> value in cell(2,2), forexample, both of sheet1,4,5,62 have same value
    M> "name"
    M> in cell(2,2). So only sheet1 change name to "name", other worksheets
    M> will
    M> get error and keep their own name.
    M> My question is, can I change these worksheets name to
    M> "name1"..."name34"
    M> base on my script? Or how can I determin whether a worksheets named
    M> as
    M> current cell(2,2) already exist before I change it?
    M> Thanks.
    M> MG
    M> "Shay Levi" <no@xxxxxx> wrote in message
    M> news:95d8089330ab78ca9b6ccefa9d40@xxxxxx
    M>

    >> Try:
    >>
    >> $xl = new-object -com excel.application
    >> $workbook = $xl.workbooks.open("D:\out\M2.xlsx")
    >> # filter workSheets by name
    >> $sheet = $workbook.worksheets | where {$_.name -eq "sheetName"}
    >> if($sheet) { "workSheet exists" }
    >>
    >> ---
    >> Shay Levi
    >> $cript Fanatic
    >> http://scriptolog.blogspot.com

    >>> Hi,
    >>> I wrote a PS script like this:
    >>> $a = New-Object -comobject Excel.Application
    >>> $a.visible = $true
    >>> $b = $a.Workbooks.Open("D:\out\M2.xlsx")
    >>> $i = 1
    >>> do {
    >>> $c = $b.worksheets.Item($i)
    >>> $c.name = $c.cells.item(2,2).value2
    >>> $i++
    >>> }while ($i -le $b.worksheets.count)
    >>> Some value may be the same, so I got error:
    >>> Exception setting "Name": "Cannot rename a sheet to the same name as
    >>> another
    >>> sheet, a referenced object library or a workbook referenced by
    >>> Visual
    >>> Basic."
    >>> At line 7, position 4
    >>> $c.name = $c.cells.item(2,2).value2
    >>> Does anybody met the same problem?
    >>> Thanks!
    >>>
    >>> MG
    >>>


      My System SpecsSystem Spec

  6. #6


    Michael Gao Guest

    Re: How to determin a worksheet exist in Excel?

    while($sheets | where {$_.name -eq "$name$counter"} ){ $counter++ }

    That's what I needed! Thank you very much.

    MG

    "Shay Levi" <no@xxxxxx> wrote in message
    news:89228ed230c488ca9c992bb4dfe2@xxxxxx

    > Hi MG,
    >
    > Try this:
    >
    >
    > $xl = new-object -com excel.application
    > #$xl.visible=$true
    > $workbooks = $xl.workbooks.open("D:\out\M2.xlsx")
    > $sheets = $workbooks.worksheets
    >
    > foreach($s in $sheets){
    > $name = $s.range("B2").value()
    > $counter=$null
    > while($sheets | where {$_.name -eq "$name$counter"} ){ $counter++ }
    > $s.name = "$name$counter"
    > $counter=$null
    > }
    >
    > $workbook.save()
    > $xl.quit()
    >
    >
    >
    > ---
    > Shay Levi
    > $cript Fanatic
    > http://scriptolog.blogspot.com
    >
    > M> Sorry for my question is not quite clearly.
    > M> I have an Excel file that contain hundreds of worksheets. I need
    > M> change each
    > M> worksheet name to their cells value (2,2). But some worksheets have
    > M> same
    > M> value in cell(2,2), forexample, both of sheet1,4,5,62 have same value
    > M> "name"
    > M> in cell(2,2). So only sheet1 change name to "name", other worksheets
    > M> will
    > M> get error and keep their own name.
    > M> My question is, can I change these worksheets name to
    > M> "name1"..."name34"
    > M> base on my script? Or how can I determin whether a worksheets named
    > M> as
    > M> current cell(2,2) already exist before I change it?
    > M> Thanks.
    > M> MG
    > M> "Shay Levi" <no@xxxxxx> wrote in message
    > M> news:95d8089330ab78ca9b6ccefa9d40@xxxxxx
    > M>

    >>> Try:
    >>>
    >>> $xl = new-object -com excel.application
    >>> $workbook = $xl.workbooks.open("D:\out\M2.xlsx")
    >>> # filter workSheets by name
    >>> $sheet = $workbook.worksheets | where {$_.name -eq "sheetName"}
    >>> if($sheet) { "workSheet exists" }
    >>>
    >>> ---
    >>> Shay Levi
    >>> $cript Fanatic
    >>> http://scriptolog.blogspot.com
    >>>> Hi,
    >>>> I wrote a PS script like this:
    >>>> $a = New-Object -comobject Excel.Application
    >>>> $a.visible = $true
    >>>> $b = $a.Workbooks.Open("D:\out\M2.xlsx")
    >>>> $i = 1
    >>>> do {
    >>>> $c = $b.worksheets.Item($i)
    >>>> $c.name = $c.cells.item(2,2).value2
    >>>> $i++
    >>>> }while ($i -le $b.worksheets.count)
    >>>> Some value may be the same, so I got error:
    >>>> Exception setting "Name": "Cannot rename a sheet to the same name as
    >>>> another
    >>>> sheet, a referenced object library or a workbook referenced by
    >>>> Visual
    >>>> Basic."
    >>>> At line 7, position 4
    >>>> $c.name = $c.cells.item(2,2).value2
    >>>> Does anybody met the same problem?
    >>>> Thanks!
    >>>>
    >>>> MG
    >>>>
    >
    >

      My System SpecsSystem Spec

How to determin a worksheet exist in Excel? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can not open an existing Excel worksheet TPGBrennan PowerShell 5 20 Aug 2009
Excel Worksheet Tabs Disappeared in vista 64 mozman_1970 Microsoft Office 6 22 May 2009
Finding the last row of and excel worksheet. GBPackerBacker VB Script 7 31 Mar 2009
WM won't show OLE Object: Microsoft Office Excel Worksheet daissy Vista mail 12 19 Nov 2007
Excel Worksheet Guy Thomas PowerShell 2 08 Nov 2006