簡體   English   中英

Powershell查找Excel單元格參考

[英]Powershell find excel cell reference

我正在使用以下powershell代碼在excel文檔中搜索字符串,並根據找到的內容返回true或false。

if (test-path $filePath) {
$wb = $xl.Workbooks.Open($filePath)
if ([bool]$xl.cells.find("German")) {$found = 1}
}

我希望能夠找到該字符串的單元格引用(如果找到),但是我無法弄清楚它或在Google上找到答案。 你能幫我嗎?

雖然有一種方法可以在整個工作簿中搜索值,但是通常在工作表上執行Range.Find方法 您正在為工作簿設置一個變量,但仍使用該應用程序作為搜索。 您應該獲得要從工作簿中搜索的工作表,並將其用作“查找”操作的目標。

以下是對PS1的一些建議修改。

$filePath = "T:\TMP\findit.xlsx"
$xl = New-Object -ComObject Excel.Application
$xl.Visible = $true
if (test-path $filePath) {
$wb = $xl.Workbooks.Open($filePath)
$ws = $xl.WorkSheets.item("sheet1")
if ([bool]$ws.cells.find("German")) 
    {
    $found = 1
    write-host $found
    write-host $ws.cells.find("German").address(0, 0, 1, 1)
    }
}

若要繼續搜索所有出現的事件,請使用Range.FindNext方法,直到您循環回到原始單元格地址。

$filePath = "T:\TMP\findit.xlsx"
$xl = New-Object -ComObject Excel.Application
$xl.Visible = $true
if (test-path $filePath) {
$wb = $xl.Workbooks.Open($filePath)
$ws = $wb.WorkSheets.item("sheet1")

$rc1 = $ws.cells.find("German")
if ($rc1) 
    {
    $found = 1
    $addr = $rc1.address(0, 0, 1, 0)
    do
        {
        $rc1 = $ws.cells.findnext($rc1)
        write-host $rc1.address(0, 0, 1, 0)
        } until ($addr -eq $rc1.address(0, 0, 1, 0))
    }
}

由於缺少很多代碼,因此很難提供比一般性更多的信息。 我用自己的測試環境填充了缺少的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM