首页 > 解决方案 > 在excel中使用Find方法后如何访问相邻单元格数据?

问题描述

我正在使用 Powershell 在 Excel 电子表格中搜索电子邮件地址。找到给定的单元格值后,如何将下一列移到右侧?

我知道我可能必须使用链接中描述的“地址方法”,但是该属性是指布尔值。下面的代码使用似乎是一个范围

这是我第一次将 Powershell 与 Excel 一起使用,如果有人可以演示如何获取$found、增加列值和获取内容,这不仅会有很大帮助,而且我将来会提出更聪明的问题。;)

   $Workbook = $Excel.Workbooks.Open($Source)
    ForEach ($Worksheet in @($Workbook.Sheets)) {
        # Find Method https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-find-method-excel
        $Found = $WorkSheet.Cells.Find($SearchText) #What
        If ($Found) {
            # Address Method https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-address-property-excel
            $BeginAddress = $Found.Address(0,0,1,1)
            #Initial Found Cell
            [pscustomobject]@{
                WorkSheet = $Worksheet.Name
                Column = $Found.Column
                Row =$Found.Row
                Text = $Found.Text
                Address = $BeginAddress
                Breach = $BreachValue
            }
            Do {
                $Found = $WorkSheet.Cells.FindNext($Found)
                $Address = $Found.Address(0,0,1,1)
                If ($Address -eq $BeginAddress) {
                    BREAK
                }
                [pscustomobject]@{
                    WorkSheet = $Worksheet.Name
                    Column = $Found.Column
                    Row =$Found.Row
                    Text = $Found.Text
                    Address = $Address
                    Breach = $BreachValue
            }                 
            } Until ($False)
        }
        Else {
            Write-Warning "[$($WorkSheet.Name)] Nothing Found!"
        }
    }
    $workbook.close($false)

标签: excelvbapowershell

解决方案


  1. 我不确定您为什么声称该Range.Address属性指的是布尔值-实际上,您是在行中引用$Found range$BeginAddress = $Found.Address(0,0,1,1)来使用它的。
  2. Range.Offset属性返回Range从指定的 偏移一定数量的行和/或列的Range

$Found.Offset(0, 1)右侧的Range1 列也是如此$Found,然后您可以访问其.Column.Text.Address等。


推荐阅读