[英]Find a specific value in 2nd Column and distinct all its 1st column using excel
[英]Compare Column in 2 excel and If matches, copy the respective other column value from 2nd excel to 1st excel
這應該工作:
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $false
$wb1 = $excel.Workbooks.Open( "C:\excel1.xlsx", [System.Type]::Missing, $false )
$wb2 = $excel.Workbooks.Open( "C:\excel2.xlsx", [System.Type]::Missing, $true )
$ws1 = $wb1.WorkSheets.item(1)
$ws2 = $wb2.WorkSheets.item(1)
[void]$ws1.Range( 'A:A' ).EntireColumn.Copy()
$insertRange = $ws1.Range( 'A:A' ).EntireRow
[void]$ws1.Range( 'A:A' ).Insert( [Microsoft.Office.Interop.Excel.XlInsertShiftDirection]::xlShiftToRight )
[void]$ws1.Range( 'A:A' ).EntireColumn.ClearContents()
$ws1.Cells( 1, 1 ).Value2 = 'GROUP'
$searchRange = $ws1.Range( 'B:B' ).EntireColumn
$lineCounter = 2
while( $ws2.Cells( $lineCounter, 1 ).Value2 ) {
$hostName = $ws2.Cells( $lineCounter, 2 ).Value2.Trim('" ')
$searchResult = $searchRange.Find( $hostName, [System.Type]::Missing, [System.Type]::Missing, [Microsoft.Office.Interop.Excel.XlLookAt]::xlWhole, [Microsoft.Office.Interop.Excel.XlSearchOrder]::xlByColumns )
if( $searchResult ) {
$group = $ws2.Cells( $lineCounter, 1 ).Value2
$ws1.Cells( $searchResult.Row, $searchResult.Column - 1 ).Value2 = $group
}
$lineCounter++
}
[void]$wb2.Close()
[void]$wb1.SaveAs("C:\excel_new.xlsx")
[void]$wb1.Close()
[void]$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.