Find cell value begins or contains using wildcards with powershell - excel

i have a trouble findiong a value in an excel sheet.
i want to get the cell number that contains a value with a string, but i need to use wildcars.
the cell that i am searching begins with B- and i am using this code:
$excel = New-Object -ComObject excel.application
$destinationPath = "C:\Users\john\Desktop\wb.xlsx"
$workbook = $excel.Workbooks.Open($destinationPath)
$sheet1 = $workbook.WorkSheets.item("Sheet1")
$sheet1.activate()
$range = $sheet1.Range("A:A").EntireColumn
$s = $range.find("B-")
write-host "Range found: " $s.address().tostring()
this returns me the first cell that contains B- .... but i need to know the first cell that begins with this. So i think i must use wildcards but i don't know how.
please, could anyone help me to achieve this??
thank you!! BR.

Here a working solution which is able to use regex as well:
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $True
$excel.DisplayAlerts = $False
$workbook = $excel.Workbooks.Open( "C:\Users\john\Desktop\wb.xlsx", [System.Type]::Missing, $false )
$worksheet = $workbook.WorkSheets.item("Sheet1")
[void]$worksheet.activate()
$searchRange = $worksheet.Range("A:A").EntireColumn
$searchFor = '^.*B\-.*$'
$foundCell = $null
foreach( $cell in $searchRange.Cells ) {
if( $cell.Value2 -match $searchFor ) {
$foundCell = $cell
break
}
}
if( $foundCell ) {
$foundCell.Value2
}
[void]$workbook.Close()
[void]$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null

Related

PowerShell - Is there any way to rearrange columns in excel without Import-Excel

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()

Formatting excel file using Powershell

I am currently trying to take a csv file generated from a VBScript, and use powershell to convert it to an xls then save it. (Using it as an offline Database file) Problem, is that one column (Column E) is for SKU's and has leading 0's that get lost during the translation. I am getting errors when trying the following. Powershell is new to me, so I could be making a simple mistake:
$xl = new-object -comobject excel.application
$xl.visible = $true
$Workbook = $xl.workbooks.open("file location.csv")
$Worksheets = $Workbooks.worksheets
$Worksheets.Columns.("E").NumberFormat = "00000000000"
$Workbook.SaveAs("file location.xls",1)
$Workbook.Saved = $True
$xl.Quit()
EDIT: I got it to work! Here's the following, if anyone has any pointers:
$excel = new-object -comobject excel.application
$excel.visible = $true
$Workbook = $excel.workbooks.open("file location.csv")
$Worksheets = $Workbooks.worksheets
$Worksheet = $Workbook.Worksheets.Item(1)
$Range = $Excel.Range("E:E").EntireColumn
$Range.NumberFormat = "00000000000"
$Workbook.SaveAs("file location.xls",1)
$Workbook.Saved = $True
$excel.Quit()
You can achieve the same thing with changing
$Worksheets.Columns.("E").NumberFormat = "00000000000"F15
to
$workbook.ActiveSheet.Columns.Item("E").NumberFormat = "00000000000"
from the first example to have less code.

Reading value from Excel sheet

$filepath = "C:\Users\Desktop\New folder\Tangent.xlsx"
$sheetname = "sheet"
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$WorkBook = $objExcel.Workbooks.Open($filepath)
$WorkBook.sheets | Select-Object -Property Name
$WorkSheet = $WorkBook.Sheets.Item($sheetname)
$myObj = [PSCustomObject][ordered]#{
john = $WorkSheet.Range("B1").Text
Rebel = $WorkSheet.Range("B2").Text
MArk = $WorkSheet.Range("B3").Text
Susan = $WorkSheet.Range("B4").Text
Patty = $WorkSheet.Range("B5").Text
}
I have hardcoded all the names into the code which is a weird way of doing it. I want it to read from the Excel directing using command. Can anyone help me please?
Create an empty hashtable and fill it as you iterate over the rows in your Excel sheet, then create the object.
$ht = #{}
$i = 1
while ($WorkSheet.Cells.Item($i, 1).Text) {
$ht[$WorkSheet.Cells.Item($i, 1).Text] = $WorkSheet.Cells.Item($i, 2).Text
$i++
}
$obj = [PSCustomObject]$ht
Untested, as I don't have Excel at hand here.

How do I embed hyperlinks from one excel file into text from another excel file with powershell

Good Evening everyone,
I have a problem that I am having some issues with and I really need some help. I took two csv files and compared them and converted them to an xls. Now the part I am confused about is how will I be able to take the hyperlinks from Column 1, Row 1 in one excel document and embed them into the text in the other document Column 1, Row2.
is there an easy way to do this? I found the follow link which left me a little confused : https://social.technet.microsoft.com/Forums/scriptcenter/en-US/123d673a-f9a7-4ae6-ae9c-d4ae8ef65015/powershell-excel-how-do-i-create-a-hyperlink-to-a-cell-in-another-sheet-of-the-document?forum=ITCG
I appreciate any guidance and help you can offer.
#Define the file path and sheet name
$FilePath= `enter
code"C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test3.csv"
$FilePath2="C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test3.xls"
$FilePath3="C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test4.xls"
$SheetName="Test3"
$SheetName2="HyperLinks"
#Compare two CSV files to look for matches
$CSV1 = import-csv -path
C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test1.csv
$CSV2 = import-csv -path
C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test2.csv
Compare-Object $CSV1 $CSV2 -property ShoppingList -IncludeEqual | where-
object {$_.SideIndicator -eq "=="}
# Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
# Enable the 'visible' property so the document will open in excel
$objExcel.Visible = $true
$objExcel.DisplayAlerts = $False
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet "Test3"
$WorkSheet = $WorkBook.sheets.item($SheetName)
# Delete data from column
[void]$WorkSheet.Cells.Item(1,2).EntireColumn.Delete()
#Auto fit everything so it looks better
$usedRange = $WorkSheet.UsedRange
$usedRange.EntireColumn.AutoFit() | Out-Null
#Save and convert to XLS
$Workbook.SaveAs("C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test3.xls",1)
$Workbook.Saved = $True
#Load
$excel = New-Object -comobject Excel.Application
$excel.Visible = $True
$workbook = $objExcel.Workbooks.Add()
$workbook.Worksheets.Item($FilePath2).Hyperlinks.Add( `
$workbook.Worksheets.Item($FilePath2).Cells.Item(1,2) , `
"" , $FilePath3, "https://community.spiceworks.com/topic/673034-powers
You can use something like this:
$excel = New-Object -comobject Excel.Application
$excel.Visible = $True
$workbook = $excel.Workbooks.Add()
$workbook.Worksheets.Item(1).Hyperlinks.Add($workbook.Worksheets.Item(1).Cells.Item(1,1) ,"" , "Sheet2!C4", "", "Link to sheet2")
Reference : Hyperlinks.Add Method
Hope it helps

renaming multiple excel worksheets with powershell

I am trying to use powershell to remane the worksheets in a workbook but the script I have only renames the same worksheet.
$xl = New-Object -comobject Excel.Application
$xl.visible = $true
$xl.DisplayAlerts = $false
$xl.sheetsInNewWorkbook = $batches
$xl.workbooks.add()
for($i=1;$i -lt $batches;$i++){
$ws1 = $ws.sheets.item($i)
$ws1.activate()
$ws1.name = "Batch$i"
}
Batches = 12
What am I doing wrong?
Tia
Andy
You didn't add the code that defines the content of the variable: $ws
I retrieved the created workbook in the variable $workbook and replaced $ws by $workbook.Sheets.Item($i) and it seems to be working fine (your code is looping only 11 times by the way).
If it doesn't answer your question please add more details.
$batches = 12;
$xl = New-Object -comobject Excel.Application
$xl.visible = $true
$xl.DisplayAlerts = $false
$xl.sheetsInNewWorkbook = $batches
$workbook = $xl.Workbooks.add()
for($i=1;$i -lt $batches;$i++){
$ws1 = $workbook.Sheets.Item($i)
$ws1.activate()
$ws1.name = "Batch$i"
}

Resources