2

The formula =LEFT(AB4,FIND(" ",AB5)-1 works perfectly in Excel, but seems to be causing errors in PowerShell where I get this error:

Exception from HRESULT: 0x800A03EC
At C:\Scripts\Excel_NUID2.ps1:21 char:1
+ $worksheet.range("AH5:AH$rows").formula = "=LEFT(AB4,FIND(" ",AB5)-1"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException 

My PowerShell Script Code;

#Open Up the Workbook#
$excel = new-object -comobject Excel.Application
$excel.visible = $false  
$workbook = 
$excel.workbooks.open("c:\Users\Jack\documents\NUID_Status_Report.xlsx") 
$worksheet = $workbook.Worksheets.Item(1)

$rows = $worksheet.range("A1").currentregion.rows.count

### Set up a filter ###
$headerRange = $worksheet.Range("a4","aj4")
$headerRange.AutoFilter() | Out-Null

#### Trims Password Expiration Date Name ###

$worksheet.range("AH4").formula = "Shortened Expiration Date"
[void]$worksheet.Cells.Item(1,1).select()
$excel.visible = $true

#### Trims Password Expiration Date Formula ###

$worksheet.range("AH5:AH$rows").formula = "=LEFT(AB4,FIND(" ",AB5)-1"
[void]$worksheet.Cells.Item(1,1).select()
$excel.visible = $true
2
  • You state 'The formula =LEFT(AB4,FIND(" ",AB5)-1 works perfectly in Excel' but that is incorrect; it is missing a closing bracket somewhere. Commented May 17, 2017 at 15:26
  • Correct, I must have missed the closing bracket by accident. The right should be =LEFT(AB4,FIND(" ",AB5)-1) Commented May 17, 2017 at 15:33

1 Answer 1

1

Quotes within a quoted string need to be doubled-up.

$worksheet.range("AH5:AH$rows").formula = "=LEFT(AB4,FIND("" "",AB5)-1)"
'you can also get rid of the inside quotes with the CHAR function
$worksheet.range("AH5:AH$rows").formula = "=LEFT(AB4, FIND(CHAR(32), AB5)-1)"

ASCII character 32 is a space. I've also added a bracket to make a legal formula.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.