首页 > 解决方案 > 在工作表中,如果在 A5 单元格中找到一个单词,则要使用 PowerShell 复制 B5 单元格值

问题描述

在 Excel 中有 2 列具有 A 和 B 值。如果在 A5 单元格中找到“勇气”一词,那么我想复制 B5 单元格的值。

如果“勇气”一词在 A10 中,则复制 B10 值(对面单元格)。我已经准备好下面的脚本来鼓起勇气,它工作正常但无法复制相反的单元格值。

$Excel = New-Object -ComObject Excel.Application 
$Workbook = $Excel.Workbooks.Open('C:\Users\Raj\Desktop\Book1.xlsx')
$workSheet = $Workbook.Sheets.Item(1)
$WorkSheet.Name
$Found = $WorkSheet.Cells.Find('courage')

If ($Found.) 

标签: powershell

解决方案


试试这个 -

$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$Excel.DisplayAlerts = $false
$Workbook = $Excel.Workbooks.Open('C:\Users\Raj\Desktop\Book1.xlsx')
$workSheet = $Workbook.Sheets.Item(1)
$column = 1
$LastRow = $workSheet.UsedRange.Rows.Count
for($row=1; $row -lt $LastRow; $row++)
{
    if ($WorkSheet.Cells.Item($row, $column).Value2 -eq "courage")
    {
        $WorkSheet.Cells.Item($row, $column+1).Value2 = $WorkSheet.Cells.Item($row, $column).Value2
    }
}
#Proceed with other operations
#else save the worksheet and workbook
#Quit excel after you are done

推荐阅读