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.
#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()
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
Related
I am trying to rearrange columns in an worksheet in excel. I am aware that Import-Excel or Import-CSV is the easier way but I do not have admin rights. If there is a workaround that would be greatly appreciated. Here is my code
$path = "C:\Users\file.xlsx"
$objExcel = New-Object -ComObject Excel.Application
$WorkBook = $objExcel.Workbooks.Open($path)
$WorkSheet = $WorkBook.sheets.item("sheet1")
$column4 = $Worksheet.Columns.Item(4).value1
$column3 = $WorkSheet.Columns.Item(3).value1
$Column4.Cut()
$Column3.Insert()
Thank you
You don't want to use .value1 since it's just a value and not a reference to a column.
The following example should cut column A and paste it before column C.
$path = "G:\example.xlsm"
$excel = New-Object -ComObject Excel.Application
$book = $excel.Workbooks.Open($path)
$sheet = $book.Sheets.Item("Sheet1")
$sheet.Columns("A:A").Cut()
$sheet.Columns("C:C").Insert()
$book.Save()
$excel.Quit()
I have tried lots of options to paste information copied from other Excel workbook into my new workbook but not success do that (the range is huge - more them 3000 lines).
Please see sample of my script:
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$objExcel.displayAlerts = $false
$Src = [Environment]::GetFolderPath('Desktop')+'\New.xlsx'
$Files = [Environment]::GetFolderPath('Desktop')+'\Org.xlsx'
$wb1 = $objExcel.workbooks.open($Files)
$Worksheetwb1 = $wb1.WorkSheets.item('Org')
$Worksheetwb1.activate()
$Range = $Worksheetwb1.Range('A1:I1').EntireColumn
$Range.Copy() | Out-Null
$wb3 = $objExcel.workbooks.open($Src)
$Worksheetwb3 = $wb3.WorkSheets.item('Dest')
$Worksheetwb3.activate()
$Worksheetwb3.Columns.item('A:I').clear()
$Range3 = $Worksheetwb3.Range('A1:I1').EntireColumn
$Worksheetwb3.Paste($Range.Value2)
$wb3.close($true)
$wb1.close($true)
$objExcel.Quit()
You're pasting into a wrong range. Worksheet.Paste() has parameters of destination and link, your code uses destination only, which should be a Range belonging to that worksheet. Therefore, the proper line should be this:
$Worksheetwb3.Paste($Range3)
Alternatively to Vesper's solution:
$Worksheetwb3.Range("A1").Paste() | Out-Null
# Paste special (as values)
$Worksheetwb3.Range("A1").PasteSpecial(-4163) | Out-Null
I have found the answer by changing the order of commands the Copy and then immediate after it the paste solved it for me.
This should be pretty simple. I'm looking to take the information from my previous question(s), which is a CSV, and place them in an existing Excel document.
Here's the existing data (in a CSV):
SO | Status | ElapsedHrs
PMTT12345678 Hit on Debra 2.5
PMTS23456789 Get rejected 4.25
PMTT87654321 Send some faxes 1.0
So I have an existing Excel sheet, where all of the SO category needs to go to column K starting at cell 6, Status goes to L starting at 6 and ElapsedHrs goes to column O starting at cell 6.
Using this and a few others as examples, but I can't figure out the syntax. Any help is appreciated, again.
Edit
So far I have this:
$Excel = New-Object -ComObject excel.application
$Excel.visible = $false
$WorkBook = $objExcel.Workbooks.Open($ExportCsv)
$WorkBook2 = $excel.Workbooks.open($Template)
$Worksheet = $Workbook.WorkSheets.item(“$ExpCsvShort”)
$ExportCsv is the name of the CSV with the full path. $ExpCsvShort' is just the filename (the name changes based on the hour and date). $Template` is the template .xslx file to which the data would be written.
$range = $WorkSheet.Range(“A2”).EntireColumn
$range.Copy() | out-null
Not sure what this should be, as I want column A (minus the header) to go to K on $Template starting at 6. Then I want C to start at L6 and D to start at O6, but I don't know the syntax.
Once I have that:
$Worksheet2 = $Workbook2.Worksheets.item(“Worklog”)
$worksheet2.activate()
$range2 = $Worksheet2.Range(“K6:K6”)
$Worksheet2.Paste($range2)
$workbook2.SaveAs($WorkLogSave)
$workbook.close($false)
$Excel.Quit()
[gc]::collect()
[gc]::WaitForPendingFinalizers()
But again, I don't know the syntax for the range here, either.
Edit 2
Here's what I ended up doing.
$Excel = New-Object -ComObject excel.application
$Excel.visible = $true
$WorkBook = $excel.Workbooks.Open($ExportCsv)
$WorkBook2 = $excel.Workbooks.open($Template)
$Worksheet = $Workbook.WorkSheets.item($ExpCsvShort)
$Worksheet.activate()
#A Range Copy
$rangeAc = $WorkSheet.Range(“A2:A26”)
$rangeAc.Copy() | out-null
#Select sheet 2
$Worksheet2 = $Workbook2.Worksheets.item(“Worklog”)
$worksheet2.activate()
#A Range Paste
$rangeAp = $Worksheet2.Range(“K6:K30”)
$Worksheet2.Paste($rangeAp)
#C Range Copy
$rangeCc = $WorkSheet.Range(“C2:C26”)
$rangeCc.Copy() | out-null
#C Range Paste
$rangeCp = $Worksheet2.Range(“O6:O30”)
$Worksheet2.Paste($rangeCp)
#D Range Copy
$rangeDc = $WorkSheet.Range(“D2:D26”)
$rangeDc.Copy() | out-null
#D Range Paste
$rangeDp = $Worksheet2.Range(“L6:L30”)
$Worksheet2.Paste($rangeDp)
$workbook2.SaveAs($WorkLogSave)
$workbook2.close($true)
$workbook.close($true)
$Excel.Quit()
[gc]::collect()
[gc]::WaitForPendingFinalizers()
Probably a pretty crappy way to do it, but it works. Also, I still have EXCEL.EXE running after everything is closed. I've read about 5 ways to kill the process, but I'm worried if I do I'll mess someone up who has another document open, so maybe I can -passthru and capture the .id and kill that instead, but I'll worry about that later I guess.
Thanks for helping, all.
And I'd love to use that gravity, but I'm not sure I understand how. Thanks!
I've been diving into how Powershell can use Excel as a COM object, have most of it down but there are two things I'd like to be able to do that I haven't been able to find anywhere, hoping someone can help.
1/ Would like to be able to script hiding a range of columns in the generated Excel spreadsheet.
2/ Would like to be able to have Excel add a border around all cells in the script as well.
Thanks!
Hiding a column:
Here is an example that you can adapt. This is hiding the first column in the active work sheet.
$file = "C:\Users\Micky\Desktop\not locked.xlsx"
[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Excel")|Out-Null
$excel = New-Object Microsoft.Office.Interop.Excel.ApplicationClass
$excel.Visible = $true
$wb = $excel.Workbooks.Open($file)
$ws = $wb.ActiveSheet
$c = $ws.Columns
$c.Item(1).hidden = $true
Cell border:
For the example I use a double border and apply to the first cell, A1.
The XlLineStyle Enum can be found here
$xlDouble = -4119
$item = $ws.Range("A1")
$item.Borders.LineStyle = $xlDouble
As the title suggested, I'm not sure how to do the equivalent of selecting only a portion of a row and insert cells (shift rows down) using Powershell. It seems like all tutorials online are about inserting the entire row or entire column which is not what I want to do.
Any ideas?
Thanks.
Try something like this:
$xl = New-Object -COM "Excel.Application"
$xl.Visible = $true
$wb = $xl.Workbooks.Open "C:\path\to\your.xlsx"
$ws = $wb.Sheets.Item(1)
$ws.Range("C5:E9").Insert(-4121)