Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

315
Views
Cambiar una cadena de texto en Excel en varios archivos con Powershell

Tengo varios archivos de Excel que tienen datos similares. Quería ejecutar un script de PowerShell en todos los archivos de Excel en una carpeta específica y reemplazar una palabra clave o varias palabras clave en la tabla y cambiarla por otra. Soy nuevo en shell, esto es lo que tengo hasta ahora. La secuencia de comandos se ejecuta y abre todos mis archivos de Excel, pero no cambia nada. Tenga en cuenta que todos los datos que necesito cambiar están en la hoja 2 (LWS)

 $Path = "C:\Users\mabrant\Downloads\Workstations (21)\Workstations\" $files = Get-ChildItem "C:\Users\mabrant\Downloads\Workstations (21)\Workstations" -Filter *.xlsx ForEach ($item in $files) { $Excel = New-Object -ComObject Excel.Application $Excel.visible = $true $Workbook = $Excel.workbooks.open($Path + [System.IO.Path]::GetFileName("$item")) $Worksheets = $Workbooks.worksheets $Worksheet = $Workbook.Worksheets.Item(2) $SearchString = "NEW" #String to Find $Range = $Worksheet.Range("S4:Y4").EntireColumn #Range of Cells to look at $Search = $Range.find($SearchString) } $Search = $Range.find($SearchString) if ($search -ne $null) { $FirstAddress = $search.Address do { $Search.value() = "Installed" # Replacement Value $search = $Range.FindNext($search) } while ( $search -ne $null -and $search.Address -ne $FirstAddress) } $WorkBook.Save() $WorkBook.Close() [void]$excel.quit() `
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Parece que estuviste muy cerca de tener un guión funcional. Creo que el problema principal es que su bloque ForEach debe incluir todo excepto $excel.quit() para que pueda guardar y cerrar cada libro de trabajo a medida que avanza.

Reformateé su código para que sea más fácil ver todo el bloque ForEach, eliminé la instrucción duplicada $Search = $Range.find($SearchString) y configuré algunas propiedades de Excel.Application en $false para que funcione mejor.

Aquí está el código actualizado:

 $Path = "C:\Users\mabrant\Downloads\Workstations (21)\Workstations\" $files = Get-ChildItem $Path -Filter *.xlsx $Excel = New-Object -ComObject Excel.Application $Excel.Visible = $false $Excel.EnableEvents = $false $Excel.DisplayAlerts = $false ForEach ($item in $files) { $Workbook = $Excel.Workbooks.Open($Path + [System.IO.Path]::GetFileName("$item")) $Worksheet = $Workbook.Worksheets.Item(2) $SearchString = "NEW" #String to Find $Range = $Worksheet.Range("S4:Y4").EntireColumn #Range of Cells to look at $Search = $Range.find($SearchString) if ($Search -ne $null) { $FirstAddress = $Search.Address do { $Search.Value() = "Installed" # Replacement Value $Search = $Range.FindNext($Search) } while ( $Search -ne $null -and $Search.Address -ne $FirstAddress) } $WorkBook.Save() $WorkBook.Close() } $Excel.Quit()
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!