excelpowershellcomobject

Powershell - Unable to add specific text in Excel Comobject


I'm attempting to add specific text to a cell in an Excel sheet using Powershell. For example:

$1,500/$2,500

The closest I can get to adding that text into a cell with the proper formatting is to remove the characters: 1500/2500. I've tried treating the cell as text and entering it as a formula.

Does not work:

$wb.Sheets(1).Range("G9").Formula = "=""$1,500""&""/""&""$2,500"""

Or

$wb.Sheets(1).Range("G9").NumberFormat = "@"
$wb.Sheets(1).Range("G9").Value = "$1,500/$2,000"

Solution

  • Use single quotes instead of double:

    $wb.Sheets(1).Range("G9").Value = '$1,500/$2,000'
    

    You can eliminate the $wb.Sheets(1).Range("G9").NumberFormat = "@" as well. It is not necessary to change the cell formatting to text.