Answered by:
How to find and replace the matching pattern inside a Excel macro file

Question
-
I have an excel macro file which contain multiple sheets in it.
What I want is I have to search for a matching string in all the sheets and replace them if match found.Note : The matching text will be present in a random cell and random sheet.
It would be like ‘\\RS-DISCISDG01\dshare\runwork\deploy_area’ and it should be replaced to ‘\\DF-USTRYFYG897\dshare\runwork\deploy_area’In the below code, am just trying to find the matching pattern and its not working as expected.
Please suggest a way to find and replace the content in excel macro file.
$before = @(Get-Process [e]xcel | %{$_.Id}) $excel = New-Object -comobject Excel.Application $ExcelId = Get-Process excel | %{$_.Id} | ?{$before -notcontains $_} $excel.Visible = $false $excel.DisplayAlerts = $false $excel.AskToUpdateLinks = $false $workbook = $excel.Workbooks.Open('D:\dinesh\worksheetArea\workbook1.xlsm') For($i = 1 ; $i -le $workbook.Sheets.count ; $i++) { $worksheet = $workbook.sheets.item($i) $search = $worksheet.usedRange.Find('ServerId::50012') If($search){ $final_list += worksheet.Name } } if (!$Workbook.saved){ $workbook.save() } $workbook.close() $excel.quit() $excel = $null [gc]::collect() [gc]::WaitForPendingFinalizers()
- Changed type Dinesh Edvin Tuesday, February 18, 2020 2:33 PM
- Edited by Dinesh Edvin Tuesday, February 18, 2020 2:36 PM
- Changed type Dinesh Edvin Tuesday, February 18, 2020 2:36 PM
Tuesday, February 18, 2020 11:03 AM
Answers
-
Here is the method with a full description of how to use it.
\_(ツ)_/
- Marked as answer by Dinesh Edvin Tuesday, February 18, 2020 2:36 PM
Tuesday, February 11, 2020 4:38 PM
All replies
-
Hi,
I have an excel macro file which contain multiple sheets in it.
What I want is I have to search for a matching string in all the sheets and replace them if match found.
Note : The matching text will be present in a random cell and random sheet.
It would be like '\\RS-DISCISDG01\dshare\runwork\deploy_area' and it should be replaced to '\\DF-USTRYFYG897\dshare\runwork\deploy_area'
In the below code, am just trying to find the matching pattern and its not working as expected.
Please suggest a way to find and replace the content in excel macro file.$File = 'D:\myproject\JVDT\Workbook\RunMaster.xlsm' $SearchText = 'RS-DISCISDG01' $replacetext = 'DF-USTRYFYG897' $Excel = New-Object -ComObject Excel.Application $Workbook = $Excel.Workbooks.Open($File) foreach ($WorkSheet in $workbook.Worksheets) { $val = $Worksheet.usedrange.Find($SearchText) Write-Host "val : $val" If($Worksheet.usedrange.Find($SearchText)) { $Workbook.Close($false) write-host "Search text Exists" # $workbook.close($false) break } } $Excel.Quit() [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
Thanks
Dinesh
- Merged by jrv Tuesday, February 18, 2020 1:01 PM DUPLICATE
Tuesday, February 11, 2020 3:48 PM -
When you crosspost the same question at the same time in different forums you should at least post links to the other crossposts along with your question to avoid for people willing to help you to make their work twice or more ...
Powershell.org: How to find and replace the matching pattern inside a Excel macro file.
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
- Edited by BOfH-666 Tuesday, February 11, 2020 3:59 PM
Tuesday, February 11, 2020 3:58 PM -
Excel has a full Find/Replace method. Look it up with your search engine. It takes one line of code to do a global search/replace.
If you cannot find it or have other issues with Excel post in the Excel developers forum for expert advice. Those guys have gotten pretty good with PowerShell and Excel.
\_(ツ)_/
- Proposed as answer by jrv Tuesday, February 18, 2020 1:03 PM
Tuesday, February 11, 2020 4:36 PM -
\_(ツ)_/
- Proposed as answer by jrv Tuesday, February 18, 2020 1:03 PM
Tuesday, February 11, 2020 4:37 PM -
Here is the method with a full description of how to use it.
\_(ツ)_/
- Marked as answer by Dinesh Edvin Tuesday, February 18, 2020 2:36 PM
Tuesday, February 11, 2020 4:38 PM -
Instead of posting this on both forums, you could have given a solution in either one forum.
- Edited by Dinesh Edvin Wednesday, February 12, 2020 8:19 AM
Wednesday, February 12, 2020 7:44 AM