0

I want to add a column after a particular column number in excel sheet using Powershell. I am able to add it at starting of sheet, but couldn't insert after a specific column.

3
  • Perhaps this can help: gallery.technet.microsoft.com/office/… Commented Jul 3, 2014 at 9:47
  • I had checked this link earlier. Can u tell me what does -4161 represents? Commented Jul 3, 2014 at 9:52
  • Scripting guy puts it well "yes, minus 4161; go figure". This article will shine some light as the PS from my previous link is a direct adaptation: blogs.technet.com/b/heyscriptingguy/archive/2005/04/11/… Commented Jul 3, 2014 at 10:06

2 Answers 2

2
#This will insert a column at column R

$Excel = New-Object -ComObject  excel.application 
$ExcelWorkSheet = $ExcelWordBook.Worksheets.Add()
$ExcelWorkSheet.Name = "TestThis"
#do other things

$ColumnSelect = $ExcelWorkSheet.Columns("R:R")
$ColumnSelect.Insert()
Sign up to request clarification or add additional context in comments.

Comments

1

Alas, I agree, I have not found neither documentation or examples :-/ .
Nevertheless here is below how to insert a column 7th and give it a name:

(Get-ChildItem "*.xlsb")|
    foreach-object {
        $xl=New-Object -ComObject Excel.Application
        $wb=$xl.workbooks.open($_)
        $ws = $wb.worksheets.Item(1)
        $ws.Columns.ListObject.ListColumns.Add(7)
        $ws.Cells.Item(1,7) ='Comment'
        $wb.Save()
        $xl.Quit()
        while([System.Runtime.Interopservices.Marshal]::ReleaseComObject([System.__ComObject]$xl)){'released'| Out-Null}
    }

Best regards

2 Comments

And to add a row: $ws.rows.Listobject.ListRows.Add(1)
And, one must clear filters before running this add rows command or it will give an error.

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.