locked
Powershell - Read data from individual excel cell and place this data into a variable RRS feed

  • Question

  • I'm trying to read data from an individual excel cell, based on the input of a serial number the user enters previously. Below is my code:

    #create excel object
    $excel = New-Object -ComObject Excel.Application
    # open Excel file
    $workbook = $excel.Workbooks.Open("C:\Users\NAME\Documents\Reports\Database.xlsx")
    $sheet = $workbook.ActiveSheet
    $column = 2
    $row = $serial
    $info = $sheet.cells.Item($column, $row).Text
    Write-Output($info)

    When this runs I receive the following error:

    Exception from HRESULT: 0x800A03EC
    At C:\Users\NAME\Documents\Reports\report_script-v0.3.ps1:27 char:1
    + $info = $sheet.cells.Item($column, $row).Text
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

    Thursday, July 23, 2020 9:52 AM

Answers

  • The cell you are asking for does not exist. "$serial" is null and must be a legal row number.


    \_(ツ)_/

    • Marked as answer by jrv Tuesday, July 28, 2020 7:26 AM
    Thursday, July 23, 2020 7:35 PM

All replies

  • The cell you are asking for does not exist. "$serial" is null and must be a legal row number.


    \_(ツ)_/

    • Marked as answer by jrv Tuesday, July 28, 2020 7:26 AM
    Thursday, July 23, 2020 7:35 PM
  • Hi,
    As this thread has been quiet for a while, so here is a quick question.Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Yang Yang

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, July 28, 2020 7:24 AM