简体   繁体   English

如何在 A 列的循环中运行,找到一个值并从 B 列打印相应的数据

[英]How to run in a loop for column A , find a value and print the corresponding data from column B

I am working on xlsx file and i need to read values from column A and display the values in column B For an example column A has 100 rows and some of them have a string.我正在处理 xlsx 文件,我需要从 A 列读取值并在 B 列中显示值。例如,A 列有 100 行,其中一些有一个字符串。 At column B (Also 100 rows) i have also values.在 B 列(也有 100 行)我也有值。 I want to run in a loop a search for all the cells in column A, Store them and print the corresponding values in column B我想循环运行搜索 A 列中的所有单元格,存储它们并在 B 列中打印相应的值

在此处输入图像描述

I want to search for # and display 1,2,7 from BI need an object that holds the values from A and object for B (For further actions)我想搜索 # 并显示 1,2,7 来自 BI 需要一个 object 来保存 A 的值和 object 来保存 B(用于进一步操作)

The code below search in all the columns and display the values.下面的代码在所有列中搜索并显示值。 What i need is to read only from a specific column.我需要的是只阅读特定专栏。 and i need an object that holds the values from A and B我需要一个 object 来保存 A 和 B 的值

$data holds the data of column A. I want to in a loop and search for data and then display the same data in the same row in column B? $data 保存 A 列的数据。我想在一个循环中搜索数据,然后在 B 列的同一行中显示相同的数据?

$ExcelFile = "C:\Temp\SharedFolder\Test.xlsx" 
$excel = New-Object -ComObject Excel.Application
$Excel.visible = $false
$Excel.DisplayAlerts = $False # Disable comfirmation prompts
$workbook = $excel.Workbooks.Open($ExcelFile)

$data = $workbook.Worksheets['Sheet1'].UsedRange.Rows.Columns[1].Value2

Doing this in Excel can be done, but takes a bit more work.可以在 Excel 中执行此操作,但需要做更多的工作。

If this is your Excel file:如果这是您的 Excel 文件:

在此处输入图像描述

$ExcelFile   = "D:\Test\Test.xlsx" 
$searchValue = '@'

$excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
$Excel.DisplayAlerts = $False # Disable comfirmation prompts
$workbook  = $excel.Workbooks.Open($ExcelFile)
$worksheet = $workbook.Worksheets.Item(1)

# get the number of rows in the sheet
$rowMax = $worksheet.UsedRange.Rows.Count

# loop through the rows to test if the value in column 1 equals whatever is in $searchValue
# and capture the results in variable $result
$result = for ($row = 1; $row -le $rowMax; $row++) {
    $val = $worksheet.Cells.Item($row, 1).Value2
    if ($val -eq $searchValue) {
        # output an object with both values from columns A and B
        [PsCustomObject]@{A = $val; B = $worksheet.Cells.Item($row, 2).Value2}
    }
}

# when done, quit Excel and remove the used COM objects from memory (important)
$excel.Quit()
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

Now you can process the objects in $result .现在您可以处理$result中的对象。 For demo just output:对于演示仅 output:

$result
A B
- -
@ 1
@ 2
@ 7

Of course, it would be far easier if you save your Excel file as CSV..当然,如果将 Excel 文件保存为 CSV 会容易得多。

$searchValue = '@'
$result = Import-Csv -Path 'D:\Test\Test.csv' -UseCulture | Where-Object { $_.A -eq $searchValue }

$result

When exporting an Excel file to Csv, Excel won't always use the comma as delimiter character.将 Excel 文件导出到 Csv 时,Excel 不会始终使用逗号作为分隔符。 That depends on your machine's local settings.这取决于您机器的本地设置。 This is the reason I added switch -UseCulture to the Import-Csv cmdlet which will make sure it uses the same delimiter character your locally installed Excel uses for its output.这就是我将 switch -UseCulture添加到 Import-Csv cmdlet 的原因,这将确保它使用本地安装的 Excel 用于其 output 的相同分隔符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果A列包含文本,则将值从B列中的单元格复制到B列中的相应项目 - If column A contains text, copy value from cell in Column B to corresponding item in Column B 遍历B列以获取A列中的每个值以查找重复项 - Loop through column B foreach value in column A to find duplicates VBA-如何从B列中对应字段等于给定值的字段中获取所有值? - VBA - How to get all values from fields in column B where corresponding fields are equal to a given value? Microsoft Excel - 对于A列中的每个唯一值,从C列返回最大值,从B返回相应的值 - Microsoft Excel - for each unique value in column A, return max value from column C and the corresponding value from B Excel-如何基于列A中的多行在列B​​中查找字符串并返回列C中的对应行? - Excel - How to find strings in Column B based on multiple rows in Column A and return the corresponding row in Column C? 取一个值表单 searchname 列并在 A 列中找到它(如果可用)然后在下一列 B 中打印主客户列 - take a value form searchname column and find it into column A if available then in next column B will print Master customer column 查找A列中的值是否包含B列的值? - Find if value in column A contains value from column B? 创建一个宏以在 B 列中查找值,如果有匹配的大小写,则复制相应的单元格值 - Create a macro to find a value in Column B and if any matching case then copy the corresponding cell value 当B列中出现相同的对应值时,计算A列中的值的时间差 - Calculating time differences in values from column A when the same corresponding value appears in column B 如何查找A列中的重复行并将B列中的相应数据放入转置的C和D列中? - Howto lookup repeating rows in Column A and place the corresponding data from column B into transposed columns C & D?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM